Storage Options
閱讀:http://developer.android.com/guide/topics/data/data-storage.html
?
主要類型有:
Shared Preferences
??? 使用鍵值對(duì)存儲(chǔ)
Internal Storage
??? 在內(nèi)存存儲(chǔ)私人數(shù)據(jù)。
External Storage
??? 在內(nèi)存存儲(chǔ)共用的數(shù)據(jù)。
SQLite Databases
??? 使用內(nèi)部數(shù)據(jù)庫(kù)。
Network Connection
??? 網(wǎng)絡(luò)連接來(lái)存儲(chǔ)。
?
使用Shared Preferences
可以通過(guò)以下方式獲取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.
可以直接在私有的空間里直接存儲(chǔ)文件,其他的程序和用戶都無(wú)法訪問(wèn)它,當(dāng)程序被卸載的時(shí)候,這些文件也會(huì)被刪除。
——————————
String FILENAME = "hello_file"; String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close();MODE_PRIVATE 會(huì)創(chuàng)建一個(gè)私有的文件。 Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.
——————————
To read a file from internal storage:
Raw的訪問(wèn)方法:
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
我們可以先檢測(cè)一下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或者更高級(jí)的,可使用getExternalFilesDir()打開一個(gè)目錄來(lái)存儲(chǔ)文件,在參數(shù)里面可以有DIRECTORY_MUSIC 或者其他的,如果為null,則會(huì)選擇屬于本程序的目錄,本程序的目錄在卸載的時(shí)候會(huì)被刪除掉。
如果使用API8以下的,則需要使用getExternalStorageDirectory()并且手動(dòng)選擇目錄。
——————————
如果想要?jiǎng)?chuàng)建能夠共享的文件,程序被刪除后文件依然存在,則可以選擇以下方法:
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.
?
使用數(shù)據(jù)庫(kù)
使用數(shù)據(jù)庫(kù)需要一個(gè)繼承SQLiteOpenHelper的類,并覆蓋oncreate方法來(lái)創(chuàng)建數(shù)據(jù)庫(kù)。
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);} }想要更好的學(xué)習(xí),可以參照官方例子:
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
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/yutoulck/p/3388490.html
總結(jié)
以上是生活随笔為你收集整理的Storage Options的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Orm图解教程
- 下一篇: [zz] 几种类间关系:继承、实现、依赖