Storage Options
閱讀:http://developer.android.com/guide/topics/data/data-storage.html
?
主要類型有:
Shared Preferences
??? 使用鍵值對存儲
Internal Storage
??? 在內存存儲私人數據。
External Storage
??? 在內存存儲共用的數據。
SQLite Databases
??? 使用內部數據庫。
Network Connection
??? 網絡連接來存儲。
?
使用Shared Preferences
可以通過以下方式獲取SharedPreferences:
??? getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
??? getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
?
使用Internal Storage
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
可以直接在私有的空間里直接存儲文件,其他的程序和用戶都無法訪問它,當程序被卸載的時候,這些文件也會被刪除。
——————————
String FILENAME = "hello_file"; String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();MODE_PRIVATE 會創建一個私有的文件。 Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.
——————————
To read a file from internal storage:
Raw的訪問方法:
If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw.<filename> resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
?
Other useful methods
getFilesDir()?
?
使用external storage
我們可以先檢測一下media的可用性:
boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState();if (Environment.MEDIA_MOUNTED.equals(state)) {// We can read and write the mediamExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {// We can only read the mediamExternalStorageAvailable = true;mExternalStorageWriteable = false; } else {// Something else is wrong. It may be one of many other states, but all we need// to know is we can neither read nor writemExternalStorageAvailable = mExternalStorageWriteable = false; }——————————
If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. By specifying the type of directory, you ensure that the Android's media scanner will properly categorize your files in the system (for example, ringtones are identified as ringtones and not music). If the user uninstalls your application, this directory and all its contents will be deleted.
If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:
/Android/data/<package_name>/files/The <package_name> is your Java-style package name, such as "com.example.android.app". If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted.
如果使用API8或者更高級的,可使用getExternalFilesDir()打開一個目錄來存儲文件,在參數里面可以有DIRECTORY_MUSIC 或者其他的,如果為null,則會選擇屬于本程序的目錄,本程序的目錄在卸載的時候會被刪除掉。
如果使用API8以下的,則需要使用getExternalStorageDirectory()并且手動選擇目錄。
——————————
如果想要創建能夠共享的文件,程序被刪除后文件依然存在,則可以選擇以下方法:
In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.
If you're using API Level 7 or lower, use getExternalStorageDirectory() to open a File that represents the root of the external storage, then save your shared files in one of the following directories:
- Music/ - Media scanner classifies all media found here as user music.
- Podcasts/ - Media scanner classifies all media found here as a podcast.
- Ringtones/ - Media scanner classifies all media found here as a ringtone.
- Alarms/ - Media scanner classifies all media found here as an alarm sound.
- Notifications/ - Media scanner classifies all media found here as a notification sound.
- Pictures/ - All photos (excluding those taken with the camera).
- Movies/ - All movies (excluding those taken with the camcorder).
- Download/ - Miscellaneous downloads.
?
使用數據庫
使用數據庫需要一個繼承SQLiteOpenHelper的類,并覆蓋oncreate方法來創建數據庫。
public class DictionaryOpenHelper extends SQLiteOpenHelper {private static final int DATABASE_VERSION = 2;private static final String DICTIONARY_TABLE_NAME = "dictionary";private static final String DICTIONARY_TABLE_CREATE ="CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +KEY_WORD + " TEXT, " +KEY_DEFINITION + " TEXT);";DictionaryOpenHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(DICTIONARY_TABLE_CREATE);} }想要更好的學習,可以參照官方例子:
For sample apps that demonstrate how to use SQLite databases in Android, see the Note Pad and Searchable Dictionary applications.
例子在本地,路徑是:android-sdk\docs\guide\samples
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/yutoulck/p/3388490.html
總結
以上是生活随笔為你收集整理的Storage Options的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Orm图解教程
- 下一篇: [zz] 几种类间关系:继承、实现、依赖