Storage Options
来源:互联网 发布:非同凡想网络 编辑:程序博客网 时间:2024/06/04 18:48
Storage Options
Data storage options are the following:
a>Shared Preferences:
Store private primitive data in key-value pairs.
b>Internal Storage:
Store private data on the device memory.
c>External Storage:
Store public data on the shared external storage.
d>SQLite Storage:
Store structured data in a private storage.
e>Network Connection
Store data on the web with your own network server.
Android provides a way for you to expose even your private data to other applications - with a content provider.
Using Shared Preferences
To get a SharedPreferences object for your application, use one of two methods:
a>getSharedPreferences()
Use this if you need multiple preferences files identified by name.
b>getPreferences()
Use this if you need only one preferences file for you activity.
To write values:
1> Call edit() to get a SharedPreferences.Editor.
2> Add values with methods such as putBoolean() and putString().
3> Commit the new values with commit().
To read values
Use SharedPreferences methods such as getBoolean() and getString().
public class PreferenceActivity extends Activity {public static final String KEY_PREFERENCE_NAME = "preferences";private final String KEY_SETTING_SILENTMODE = "com.demo.search.preferences.key.setting.silentmode";private boolean mSilentMode;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);SharedPreferences preferences = getSharedPreferences(KEY_PREFERENCE_NAME, Context.MODE_PRIVATE);mSilentMode = preferences.getBoolean(KEY_SETTING_SILENTMODE, false);}@Overrideprotected void onResume() {super.onResume();SharedPreferences.Editor editor = (Editor) getSharedPreferences(KEY_PREFERENCE_NAME, Context.MODE_PRIVATE);editor.putBoolean(KEY_SETTING_SILENTMODE, mSilentMode);editor.commit();}}
Using the 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.
To create and write a private file to the internal storage:
1> Call openFileOutput() with the name of the file and the operating mode.
2> Write to the file with write().
3> Close the stream with close().
FileOutputStream stream = null;try {String FILENAME = "hello_file";String value = "hello world!";stream = openFileOutput(FILENAME, Context.MODE_PRIVATE);stream.write(value.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {stream.close();} catch (IOException e) {e.printStackTrace();}}To read a file from internal storage:
1> Call openFileInput() and pass it the name of the file to read.
2> Read bytes from the file with read().
3> Then close the stream with close().
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).
Saving cache files
If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a file that represents the internal directory where your application should save temporary cache files.
When the device is low on internal storage space, Android may delete these cache files to recover space. However, you should not rely on the system to clean up these files for you. You should always maintain the cache files yourself and stay within a reasonable limit of space consumed, such as 1MB. When the user uninstalls your application, these files are removed.
Other useful methods
getFilesDir()
Gets the absolute path to the filesystem directory where your internal files are saved.
getDir()
Creates (or opens an existing) directory within your internal storage space.
deleteFile()
Deletes a file saved on the internal storage.
fileList()
Returns an array of files currently saved by your application.
Using the External Storage
External storage can become unavailable it the user mounts the external storage on a computer or removes the media, there's no security enforces upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them.
Getting access to external storage
In order to read or write files to the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions.
If you need to both read and write files, the you need to request only the WRITE_EXTERNAL_STORAGE permission, because it implicitly requires read access as well.
Checking media availability
Before you do any work with the external storage, you should always call getExternalStorageState() to check whether the media is available.
Saving files that can be shared with other apps
Saving files that are app-private
If you are handling files that are not intended for other apps to use, you should use a private storage directory on the external storage by calling getExternalFilesDir().
When the user uninstalls your application, this directory and all its contents are deleted.
Although the directories provided by getExternalFilesDir() and getExternalFileDirs() are not access all files by the MediaStore content provider, other apps with the READ_EXTERNAL_STORAGE permission can access all files on the external storage, including these.
Saving cache files
To open a File that represents the external storage directory where you should save cache files, call getExternalCacheDir(). If the user uninstalls your application, these files will be automatically deleted.
Using Databases
Any databases you create will be accessible by name to any class in the application, but not outside the application.
The recommended method to create a new SQLite database is to create a subclass of SQLiteOpenHelper and override the onCreate() method, in which you can execute a SQLite command to create tables in the database.
If you implement a content provider, you must include a unique ID using the BaseColumns._ID constant.
Database debugging
Using a Network Connection
- Storage Options
- Storage Options
- Storage Options
- Ehcache Storage Options
- Ehcache Storage Options
- android之Storage Options
- android-Storage Options
- Designing Data Storage Architecture-Azure Storage Options
- Android Storage Options(存储方式)
- Storage Options(数据存储)
- 【Android API Guides 简译(三)】Data Storage--Storage Options
- 【安卓】Android API 指南之数据存储(Data Storage)之存储选项(Storage Options)
- options
- Options
- options
- OPTIONS
- options
- options
- c语言实现面向对象之继承,多态
- Java 异常处理的误区总结
- 在VS2010中应用SIFT(C)源码
- 如何防止模拟的http的恶意请求?
- IOS 网络开发NSURLSession(四)UploadTask(上传数据+图片)
- Storage Options
- JAVA进程类中,sleep,wait,join,yield有何差别?并说明进城有哪几种状态
- Qt 5 设置窗体无边框且在任务栏能够看到图标
- redis 数据类型详解 以及 redis适用场景场合
- Java第二课——多态继承
- 磁盘中的文件文件要怎么恢复
- 阿里2014校招笔试题
- 通过SparkR在R上运行Spark
- socket阻塞与非阻塞,异步与同步