android数据存储

来源:互联网 发布:ubuntu chrome可用源 编辑:程序博客网 时间:2024/05/13 20:01
永久保存数据的方法:
1.Shared Preferences 以键值对的形式存储基本数据类型( booleans, floats, ints, longs, and strings),存储的数据在限制在一个application(一个package)内部使用
 
2.Internal Storage 将私有文件存储到内部存储器中。这些文件是一个application私有的,其他application无法访问到

3.External Storage 将公共数据存储到外部存储器中

4.SQLite Databases
Store structured data in a private database.
5.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 . A content provider is an optional component that exposes read/write access to your application data, subject to whatever restrictions you want to impose

Using Shared Preferences

获取一个 SharedPreferences 对象,两种方法:

  • getSharedPreferences() - 有多个 preferences files 时使用该方法。该方法的第一个参数指定想要操作的preferences file 的名字
  • getPreferences() - 只有一个preferences file时使用该方法。Because this will be the only preferences file for your Activity, you don't supply a name.

获取对象之后,就可以调用对象的方法读取preferences file中的数据和写入数据到preferences file

读取: 使用 SharedPreferences 的 getBoolean() getString() 方法

写入:

  1. 调用 edit() 方法获取 SharedPreferences.Editor 对象.
  2. 使用 putBoolean() and putString() 写入值 .
  3. Commit the new values with commit() :使用该方法提交新写入的值

看下面的示例:

 

view plain copy to clipboard print ?
  1. public   class  Calc  extends  Activity {   
  2.     public   static   final  String PREFS_NAME =  "MyPrefsFile" ;   
  3.    
  4.     @Override    
  5.     protected   void  onCreate(Bundle state){            
  6.        super .onCreate(state);   
  7.        . . .   
  8.    
  9.        // Restore preferences    
  10.        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0 );   
  11.        boolean  silent = settings.getBoolean( "silentMode"  false );   
  12.        setSilent(silent);   
  13.     }   
  14.    
  15.     @Override    
  16.     protected   void  onStop(){   
  17.        super .onStop();   
  18.    
  19.       // We need an Editor object to make preference changes.    
  20.       // All objects are from android.context.Context    
  21.       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0 );   
  22.       SharedPreferences.Editor editor = settings.edit();   
  23.       editor.putBoolean("silentMode" , mSilentMode);   
  24.    
  25.       // Commit the edits!    
  26.       editor.commit();   
  27.     }   
  28. }  
[java] view plaincopy
  1. public class Calc extends Activity {   
  2.     public static final String PREFS_NAME = "MyPrefsFile";   
  3.    
  4.     @Override   
  5.     protected void onCreate(Bundle state){            
  6.        super.onCreate(state);   
  7.        . . .   
  8.    
  9.        // Restore preferences   
  10.        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   
  11.        boolean silent = settings.getBoolean("silentMode"false);   
  12.        setSilent(silent);   
  13.     }   
  14.    
  15.     @Override   
  16.     protected void onStop(){   
  17.        super.onStop();   
  18.    
  19.       // We need an Editor object to make preference changes.   
  20.       // All objects are from android.context.Context   
  21.       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);   
  22.       SharedPreferences.Editor editor = settings.edit();   
  23.       editor.putBoolean("silentMode", mSilentMode);   
  24.    
  25.       // Commit the edits!   
  26.       editor.commit();   
  27.     }   
  28. }  

Using the Internal Storage

私有文件的存储。用户卸载application后该种数据会被移除

将私有文件写入到内存中:

  1. 调用 openFileOutput() 方法获取文件的输出流。在方法的参数中指定文件名和操作模式
  2. 使用 write() 方法写入
  3. 关闭流: close() .

示例:

view plain copy to clipboard print ?
  1. String FILENAME =  "hello_file" ;   
  2. String string = "hello world!" ;   
  3.    
  4. FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);   
  5. fos.write(string.getBytes());   
  6. fos.close();  
[java] view plaincopy
  1. String FILENAME = "hello_file";   
  2. String string = "hello world!";   
  3.    
  4. FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);   
  5. fos.write(string.getBytes());   
  6. fos.close();  

可用模式有:MODE_PRIVATE , MODE_APPEND MODE_WORLD_READABLE MODE_WORLD_WRITEABLE .

从内存中读取文件到程序中:

  1. 调用openFileInput() 方法获取输入流。方法参数指定文件名
  2. 使用read() 方法读取.
  3. 关闭流: close() .

在编译期读取只读的静态文件:

要把文件放在 res/raw/目录下, 使用openRawResource(R.raw.<filename> ) 方法打开文件,该方法返回一个输入流,使用这个流就可以读取文件内容了

Saving cache files

 

如果只想暂时性的保存数据而不是长久保存

 使用 getCacheDir() 方法打开一个 File (application存放临时缓存数据的路径)

当设备内存不足时,系统可能会自动删除这类数据以释放内存空间。但是你不能依靠这个可能性,你要保证cache文件不能占用过多的内存空间。当application被卸载,这部分数据被移除

Other useful methods

getFilesDir()
Gets the absolute path to the filesystem directory where your internal files are saved.
获得基于存储 internal files 目录的完整路径(不同的文件系统获得路径会不同)
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.
获得一个application存储的所有文件,并存储在数组中

Using the External Storage

外部存储。这些文件可以在设备之间随意的读写,只要设备的系统与android兼容。容易丢失,所以操作的时候要格外小心

用于存储的storage有:可移动的media storage(如SD卡)和不可移动的storage

 

Checking media availability

检查media是否可用

media在使用之前要先检查是否可用,调用 getExternalStorageState() 方法获取media的状态。用法如下:

 

view plain copy to clipboard print ?
  1. boolean  mExternalStorageAvailable =  false ;   
  2. boolean  mExternalStorageWriteable =  false ;   
  3. String state = Environment.getExternalStorageState();   
  4.    
  5. if  (Environment.MEDIA_MOUNTED.equals(state)) {   
  6.     // We can read and write the media    
  7.     mExternalStorageAvailable = mExternalStorageWriteable = true ;   
  8. else   if  (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {   
  9.     // We can only read the media    
  10.     mExternalStorageAvailable = true ;   
  11.     mExternalStorageWriteable = false ;   
  12. else  {   
  13.     // Something else is wrong. It may be one of many other states, but all we need    
  14.     //  to know is we can neither read nor write    
  15.     mExternalStorageAvailable = mExternalStorageWriteable = false ;   
  16. }  
[java] view plaincopy
  1. boolean mExternalStorageAvailable = false;   
  2. boolean mExternalStorageWriteable = false;   
  3. String state = Environment.getExternalStorageState();   
  4.    
  5. if (Environment.MEDIA_MOUNTED.equals(state)) {   
  6.     // We can read and write the media   
  7.     mExternalStorageAvailable = mExternalStorageWriteable = true;   
  8. else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {   
  9.     // We can only read the media   
  10.     mExternalStorageAvailable = true;   
  11.     mExternalStorageWriteable = false;   
  12. else {   
  13.     // Something else is wrong. It may be one of many other states, but all we need   
  14.     //  to know is we can neither read nor write   
  15.     mExternalStorageAvailable = mExternalStorageWriteable = false;   
  16. }  

一般常见的状态有:可读写、只读和不可用

 getExternalStorageState() 还会返回其他更多的状态,比如media有没有共享、安装是否正常、卸载时是否出错等等,以此来通知用户设备的状态

Accessing files on external storage

当你使用Level8或更高的API时, 使用getExternalFilesDir()  打开一个 File (表示external storage中本次数据存储的目录)。

不传递参数时表示external storage的根目录。有参数则对应着指定的目录类型,例如 DIRECTORY_MUSIC andDIRECTORY_RINGTONES ,分别代表音乐目录和铃声目录。如果指定的目录不存在,就会创建该目录。通过指定目录类型可以让android 多媒体扫描器给你的文件分类。如果你的application被用户卸载,这种方式存储的目录和内容会被移除

如果使用Level7或更低版本的API,使用 getExternalStorageDirectory() , 打开 File 所代表的external storage的根目录。

还需要把数据写在下面的目录中:
/Android/data/<package_name>/files/
用户使用Level8或更高版本的API卸载这个版本的应用时,上面的目录和目录中的内容会被移除

Saving files that should be shared

如果想保存与应用无关的文件,也就是说application卸载后保存的文件仍然保留,就要把文件保存在 external storage的公共目录中,这些目录位于 external storage的根目录,比如 Music/, Pictures/ , Ringtones/等

当你使用Level8或更高的API时,使用  getExternalStoragePublicDirectory() 方法,传递参数DIRECTORY_MUSIC DIRECTORY_PICTURES DIRECTORY_RINGTONES ,就可以把文件保存在指定目录。目录不存在则创建

如果使用Level7或更低版本的API , 使用 getExternalStorageDirectory() 方法打开 File 所代表的 external storage的根目录, 把共享文件存储到下面的目录中一个即可:

  • 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.

Saving cache files

如同上面提到的,根据不同的API版本使用不同的方法:

高版本API: 使用getExternalCacheDir()

低版本API: getExternalStorageDirectory() 打开根目录,cache data 写到/Android/data/<package_name>/cache/目录中

Using Databases

 android完全支持SQLite 数据库

经典的创建SQLite数据库的方法是:构造SQLiteOpenHelper 的子类并覆盖onCreate() 方法,就可以在方法中使用SQLite命令:

 

view plain copy to clipboard print ?
  1. public   class  MyDbOpenHelper  extends  SQLiteOpenHelper {   
  2.    
  3.     private   static   final   int  DATABASE_VERSION =  2 ;   
  4.     private   static   final  String DICTIONARY_TABLE_NAME =  "dictionary" ;   
  5.     private   static   final  String DICTIONARY_TABLE_CREATE =   
  6.                 "CREATE TABLE "  + DICTIONARY_TABLE_NAME +  " ("  +   
  7.                 KEY_WORD + " TEXT, "  +   
  8.                 KEY_DEFINITION + " TEXT);" ;   
  9.    
  10.     DictionaryOpenHelper(Context context) {   
  11.         super (context, DATABASE_NAME,  null , DATABASE_VERSION);   
  12.     }   
  13.    
  14.     @Override    
  15.     public   void  onCreate(SQLiteDatabase db) {   
  16.         db.execSQL(DICTIONARY_TABLE_CREATE);   
  17.     }   
  18. }  
[java] view plaincopy
  1. public class MyDbOpenHelper extends SQLiteOpenHelper {   
  2.    
  3.     private static final int DATABASE_VERSION = 2;   
  4.     private static final String DICTIONARY_TABLE_NAME = "dictionary";   
  5.     private static final String DICTIONARY_TABLE_CREATE =   
  6.                 "CREATE TABLE " + DICTIONARY_TABLE_NAME + " (" +   
  7.                 KEY_WORD + " TEXT, " +   
  8.                 KEY_DEFINITION + " TEXT);";   
  9.    
  10.     DictionaryOpenHelper(Context context) {   
  11.         super(context, DATABASE_NAME, null, DATABASE_VERSION);   
  12.     }   
  13.    
  14.     @Override   
  15.     public void onCreate(SQLiteDatabase db) {   
  16.         db.execSQL(DICTIONARY_TABLE_CREATE);   
  17.     }   
  18. }  

 Android中使用SQLite 数据库的例子参见 Note Pad 和Searchable Dictionary 。

Android中SQLite 数据库的调试工具:sqlite3

Using a Network Connection

You can use the network (when it's available) to store and retrieve data on your own web-based services. To do network operations, use classes in the following packages: