一、內部存儲
Android應用的內部存儲是應用私有的文件存儲空間,在設備上具有高度的安全性。只有當前應用才能訪問這些文件,其他應用或用戶無法訪問。應用通過Context.getFilesDir()方法獲得這個目錄。以下是內部存儲的一些特點:
1.文件存儲在設備上的私有空間下,只有當前應用才能訪問
2.存儲數據的大小受到設備可用存儲空間的限制,因此適用於少量數據存儲
3.當用戶卸載應用時,應用私有的內部存儲空間也會被刪除
二、外部存儲
Android應用的外部存儲是設備上所有應用共有的文件存儲空間。它適用於存儲大量數據並且這些數據需要在不同應用間共享的場景。此外,外部存儲對於一些需要長時間保存的文件(例如用戶錄製的視頻)也非常適合。以下是外部存儲的一些特點:
1.應用可以通過Context.getExternalFilesDir()方法獲得外部存儲目錄下的一個私有目錄,在該目錄下存儲的數據只能由該應用訪問
2.外部存儲還包括公有目錄,其中包括Download、Pictures、Music、Movies、DCIM、Documents等目錄,存儲在公有目錄下的數據可被所有應用訪問
3.存儲在外部存儲中的數據可能會被用戶刪除或設備清理,因此開發人員需要注意備份和數據恢復等問題
// 內部存儲 String fileName = "internal.txt"; String content = "hello world!"; FileOutputStream fos = null; try { fos = openFileOutput(fileName, MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); } // 外部存儲 String fileName = "external.txt"; String content = "hello world!"; File file = new File(getExternalFilesDir(null), fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(content.getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); }
三、SharedPreferences
SharedPreferences可用於存儲輕量級的簡單數據,例如應用設置、用戶偏好等。它通過鍵值對的形式存儲數據,並且存儲在設備上的xml文件中。以下是SharedPreferences的一些特點:
1.應用可以通過Context.getSharedPreferences()方法獲得一個SharedPreferences對象,對象可被多個組件共享
2.存儲的數據不適合大量或敏感數據
3.當用戶卸載應用時,SharedPreferences中的數據也會被刪除
SharedPreferences sp = getSharedPreferences("my_sp", MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("username", "john"); editor.apply(); String username = sp.getString("username", "");
四、資料庫存儲
SQLite是Android系統默認的關係型資料庫,可用於存儲大量複雜的結構化數據。它提供了標準的SQL語句操作,結合ContentProvider可方便地對外提供數據。以下是SQLite的一些特點:
1. SQLite的資料庫文件存儲在設備上的內部或外部存儲,但它可以通過ContentProvider和ContentResolver對外提供數據介面,數據可以被其他應用訪問
2. SQLite具有ACID事務,可確保數據的完整性和一致性
3. 當用戶卸載應用時,資料庫中的數據也會被刪除
public class MyDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "my_db"; private static final int DB_VERSION = 1; public static final String TABLE_NAME = "student"; public static final String COL_ID = "_id"; public static final String COL_NAME = "name"; public static final String COL_AGE = "age"; public MyDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql = "CREATE TABLE " + TABLE_NAME + "(" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_NAME + " TEXT NOT NULL," + COL_AGE + " INTEGER);"; sqLiteDatabase.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; sqLiteDatabase.execSQL(sql); onCreate(sqLiteDatabase); } } MyDatabaseHelper helper = new MyDatabaseHelper(context); SQLiteDatabase db = helper.getReadableDatabase(); ContentValues values = new ContentValues(); values.put(MyDatabaseHelper.COL_NAME, "Tom"); values.put(MyDatabaseHelper.COL_AGE, 18); long rowId = db.insert(MyDatabaseHelper.TABLE_NAME, null, values); Cursor cursor = db.query( MyDatabaseHelper.TABLE_NAME, new String[]{MyDatabaseHelper.COL_ID, MyDatabaseHelper.COL_NAME, MyDatabaseHelper.COL_AGE}, null, null, null, null, null ); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(MyDatabaseHelper.COL_ID)); String name = cursor.getString(cursor.getColumnIndex(MyDatabaseHelper.COL_NAME)); int age = cursor.getInt(cursor.getColumnIndex(MyDatabaseHelper.COL_AGE)); }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/271528.html