Android 存储方式比较多,常用的有4中存储,他们分别是
- SharedPreferences存储方式
- 文件存储
- 数据库存储
- ContentProvider存储
SharedPreferences存储方式
常常用来进行一些简单的数据本地存储,用来保存简单的配置信息。例如用户登录信息,设置的信息等等。它是基于Map键值对来进行存储的。但是使用SharedPreferences是有些限制的,只能在同一个包内使用,不能在不同的包之间使用。而且只能对一些简单数据进行保存
private static SharedPreferences mSharedPreferences;
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//保存String类型信息
public static void set(String key, String value) {
Editor editor = getPreferences().edit();
editor.putString(key, value);
apply(editor);
}
//读取String类型信息
public static String get(String key, String defValue) {
return getPreferences().getString(key, defValue);
}
文件存储
文件存储实际上就是对文件IO的操作。文件存储操作的一般步骤为:
- 创建并打开文件(如果文件存在则打开文件)
- 对打开后的文件进行读写操作
打开文件主要用到了openFileInput()和openFileOutput() 两个方法,打开之后将返回FileInputStream或FileOutputStream文件流。然后对流进行读写操作。下面以文本文件的读写为例来说明
/**
* 写文本文件 在Android系统中,文件保存在 /data/data/PACKAGE_NAME/files 目录下
*
* @param context
* @param msg
*/
public static void write(Context context, String fileName, String content) {
if (content == null)
content = "";
try {
FileOutputStream fos = context.openFileOutput(fileName,
Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文本文件
*
* @param context
* @param fileName
* @return
*/
public static String read(Context context, String fileName) {
try {
FileInputStream in = context.openFileInput(fileName);
return readInStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String readInStream(InputStream inStream) {
try {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int length = -1;
while ((length = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, length);
}
outStream.close();
inStream.close();
return outStream.toString();
} catch (IOException e) {
Log.i("FileTest", e.getMessage());
}
return null;
}
数据库存储
Android自带有SQLite数据库引擎。SQLite是轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能。现在的主流移动设备像Android、iPhone等都使用SQLite作为复杂数据的存储引擎。
Android 中主要通过sqliteopenhelper类 和sqlitedatabase类,Cursor类来对数据库进行操作。其中SQLiteOpenHelper类是一个辅助类,主要用于产生一个数据库对象并对数据库进行版本管理。其中的主要方法包括
- private SQLiteDatabase mDatabase;//DB
- public abstract void onCreate(SQLiteDatabase db);//创建DB
- public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);//版本更新
- public SQLiteDatabase getReadableDatabase()//对DB进行读操作
- public SQLiteDatabase getWritableDatabase()//对DB进行写操作
而对于数据库本身的增删改查操作是有SQLiteDatabase类的对象(可以看成数据库对象)完成的。对数据库的操作分为两种方式:一种是直接通过执行SQL语句来操作,
sqlite.execSQL(“SQL语句”);
sqlite.close();
举个栗子
public void deleteWithExecSQL(int id) {
SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
String sql = ("delete from " + DatabaseHelper.FRIENDS_TABLE_NAME + " where _id=?");
sqlite.execSQL(sql, new Integer[] { id });
sqlite.close();
}
public void deleteWithDB (int id){
SQLiteDatabase sqLite = dbHelper.getWritableDatabase();
sqLite.delete(DatabaseHelper.FRIENDS_TABLE_NAME,"WHERE _id="+id,null);
}
至于SQL语法,这里就不详细介绍了,自己GOOGLE。
ContentProvider内容提供器
使用ContentProvider共享数据的好处是统一了数据访问方式。就好比将上述的几种存储方式统一了起来,可能我需要的数据在DB中,我可以通过内容提供器来获取数据;可能我需要的数据在SharePreference中,不嫌麻烦我也可以通过内容提供器来获取。
这里提供一篇CSDN博客,将的比较详细。http://blog.csdn.net/faith_boys/article/details/8917395