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] 几种类间关系:继承、实现、依赖