android 应用开发时, 数据存储是必不可少的。 android 开发中有以下几种存储数据的方式:1. Shared Preferences (以key-value的形式存储私有的数据). 2. Internal Storage (在设备的内存中存储私有数据)。3. External Storage (在共享的外存中存储公共的数据) 。4. SQLite Databases (在私有的数据库中, 保存结构化的数据)。5. Network Connection (把数据保存在网络服务器上)。
Using Shared Preferences
SharedPreferences类可以让你保存和检索key-value形式的数据,在应用中获取SharedPreferences对象, 有两种方式:1. getSharedPreferences()。 2. getPreferences()。
保存数据的步骤:1. 调用edit()方法, 得到SharedPreferences.Editor 对象。 2. 增加值通过 putString()或者putBoolean()方法。 3. 调用commit()方法, 将修改提交。
private static final String FILENAME = "MyPrefsFile";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_data_storage); SharedPreferences sp = getSharedPreferences(FILENAME, Context.MODE_PRIVATE); String name = sp.getString("name", ""); Log.e(TAG, "onCreate: name = " + name); }
@Override protected void onStop() { super.onStop(); SharedPreferences sp = getSharedPreferences(FILENAME, Context.MODE_PRIVATE); SharedPreferences.Editor edit = sp.edit(); edit.putString("name", "LuoWang"); edit.commit(); }
保存数据到SharedPreferences并打印结果。
Using the Internal Storage
创建和写file到内部存储中的步骤:1. 调用openFileOutput()方法, 传入一个文件名称和操作模式, 该方法会返回一个FileOutputStream对象。2. 往文件中写内容, 需要调用write()方法。 3. 关闭流用close()方法。
private static final String INTERNAL_FILENAME = "MyInFile";
private void saveInInternalStorage() { try { FileOutputStream fos = openFileOutput(INTERNAL_FILENAME, Context.MODE_PRIVATE); fos.write("Hello World!".getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "saveInInternalStorage: error info = " + e.toString()); } }
对应写文件, 读取文件步骤:1. 调用openFileInput()方法, 该方法返回一个FileInputStream对象。 2. 通过read()方法将内容读出来。
查看文件中保存的内容:
private void findIntegerStorage() { try { byte[] b = new byte[100]; FileInputStream fis = openFileInput(INTERNAL_FILENAME); String result = ""; int byteLength = 0; while ((byteLength = fis.read(b)) != -1) { Log.e(TAG, "findIntegerStorage: byteLength = " + byteLength); Log.e(TAG, "findIntegerStorage: print = " + new String(b, 0, byteLength)); result += new String(b, 0, byteLength); } fis.close(); Log.e(TAG, "findIntegerStorage: result = " + result); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "findIntegerStorage: error info = " + e.toString()); } }
Using the External Storage
所有的应用都可以访问在手机外存中的文件,用户可以移除它们。
Getting access to external storage
为了保证可以正常访问到外存, 你的应用还必须具备访问外存的权限, 只读权限:READ_EXTERNAL_STORAGE。只写权限:WRITE_EXTERNAL_STORAGE
<manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ... </manifest>Checking media availbility
在你使用外存之前, 最好先检查一下外部存储的状态。getExternalStorageState() 该方法将返回外存的状态。
Saving files that can be shared with other apps
将文件保存在可以供别的app共享的地方。通过getExternalStoragePublicDirector(), 该方法将返回一个公共的文件目录。在该目录中的文件对手机上所有的app都是公共的。
Saving files that are app-private
将文件保存成私有的, 通过getExternalFilesDir()。android 4.4以后, 外存上面的私有文件可以不用外存访问权限。默认可以访问。
<manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" /> ... </manifest>Saving cache files
外存上的缓存目录, getExternalCacheDir()。