android app存储数据一般有以下几种选择:
1,shared-preference(共享首选项)
这种存储方式的本质就是将个个键值对存在/data/data/apppackage下的xml文件中。一般用来存app的一些状态信息。
//存
SharedPreferences settings = this.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor localEditor = settings.edit();
localEditor.putInt("pic_index", pic_index);
localEditor.commit();
//取
String pic_path = settings.getString("pic_path", "");
2,sqlite
android支持的轻量级数据库。db文件保存在/data/data/apppackage下。
有两个重要的类可以帮我们使用sqlite数据库,那就是SQLiteOpenHelper和SQLiteDatabase。
SQLiteOpenHelper是一个抽象类,用于创建数据库和版本管理。实例化时必须实现的两个方法:
onCreate:当数据库第一次被建立的时候执行,例如创建表,初始化数据等。
onUpgrade:当数据库需要被更新时执行,例如删除旧表,创建新表。
SQLiteDatabase用于操作数据库的增删改查。通过getWritableDatabase()和getReadableDatabase()可以获取数据库实例。
/data下面空间是比较有限的,所以以上两种存储方式都不要指望能存很占空间的数据。
package com.box.push.demo;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class UsrDBHelper extends SQLiteOpenHelper {
private final static int VERSION = 1;
private final static String DB_NAME = "baohe.db";
private final static String TABLE_NAME = "baohe_usr";
private final static String CREATE_TBL = "create table if not exists baohe_usr(index_ integer primary key autoincrement,openid text,nickname text,imagenum integer)";
private SQLiteDatabase db;
// SQLiteOpenHelper子类必须要的一个构造函数
public UsrDBHelper(Context context, String name, CursorFactory factory,
int version) {
// 必须通过super 调用父类的构造函数
super(context, name, factory, version);
}
// 数据库的构造函数,传递三个参数的
public UsrDBHelper(Context context, String name, int version) {
this(context, name, null, version);
}
// 数据库的构造函数,传递一个参数的, 数据库名字和版本号都写死了
public UsrDBHelper(Context context) {
this(context, DB_NAME, null, VERSION);
}
// 回调函数,第一次创建时才会调用此函数,创建一个数据库
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
Log.d(Tools.LOG_TAG, "Create Database");
db.execSQL(CREATE_TBL);
}
// 回调函数,当你构造DBHelper的传递的Version与之前的Version不同时调用此函数
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(Tools.LOG_TAG, "update Database");
}
// 插入方法
public void insert(ContentValues values) {
// 获取SQLiteDatabase实例
SQLiteDatabase db = getWritableDatabase();
// 插入数据库中
db.insert(TABLE_NAME, null, values);
db.close();
}
// 插入方法
public void insertObj(UsrObj usrObj) {
ContentValues values = new ContentValues();
values.put("openid", usrObj.getOpenID());
values.put("nickname", usrObj.getNickName());
values.put("imagenum", usrObj.getImageNum());
// 获取SQLiteDatabase实例
SQLiteDatabase db = getWritableDatabase();
// 插入数据库中
db.insert(TABLE_NAME, null, values);
db.close();
}
// 查询全部方法
public List<UsrObj> queryAll() {
SQLiteDatabase db = getReadableDatabase();
String orderBy = "index_";
ArrayList<UsrObj> list = new ArrayList<UsrObj>();
// 获取Cursor
Cursor c = db.query(TABLE_NAME, null, null, null, null, null, orderBy,
null);
while (c.moveToNext()) {
UsrObj usrObj = new UsrObj();
usrObj.setIndex(c.getInt(c.getColumnIndex("index_")));
usrObj.setOpenID(c.getString(c.getColumnIndex("openid")));
usrObj.setNickName(c.getString(c.getColumnIndex("nickname")));
usrObj.setImageNum(c.getInt(c.getColumnIndex("imagenum")));
list.add(usrObj);
}
db.close();
return list;
}
// 根据index查询
public UsrObj queryByIndex(String index) {
String[] columns = new String[] { "index_", "openid", "nickname",
"imagenum" };
String selection = "index_=?";
String[] selectionArgs = new String[] { index };
String orderBy = "index_";
// db.query(table, columns, selection, selectionArgs, groupBy,having,
// orderBy, null);
SQLiteDatabase db = getReadableDatabase();
// 获取Cursor
Cursor c = db.query(TABLE_NAME, columns, selection, selectionArgs,
null, null, orderBy, null);
UsrObj usrObj = null;
if (c.moveToNext()) {
usrObj = new UsrObj();
usrObj.setIndex(c.getInt(c.getColumnIndex("index_")));
usrObj.setOpenID(c.getString(c.getColumnIndex("openid")));
usrObj.setNickName(c.getString(c.getColumnIndex("nickname")));
usrObj.setImageNum(c.getInt(c.getColumnIndex("imagenum")));
}
db.close();
return usrObj;
}
// 根据openid查询
public UsrObj queryByOpenid(String openid) {
String[] columns = new String[] { "index_", "openid", "nickname",
"imagenum" };
String selection = "openid=?";
String[] selectionArgs = new String[] { openid };
String orderBy = "index_";
SQLiteDatabase db = getReadableDatabase();
// 获取Cursor
Cursor c = db.query(TABLE_NAME, columns, selection, selectionArgs,
null, null, orderBy, null);
UsrObj usrObj = null;
if (c.moveToNext()) {
usrObj = new UsrObj();
usrObj.setIndex(c.getInt(c.getColumnIndex("index_")));
usrObj.setOpenID(c.getString(c.getColumnIndex("openid")));
usrObj.setNickName(c.getString(c.getColumnIndex("nickname")));
usrObj.setImageNum(c.getInt(c.getColumnIndex("imagenum")));
}
db.close();
return usrObj;
}
// 根据index 来删除数据
public void deleteByIndex(int index) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, "index_=?",
new String[] { String.valueOf(index) });
db.close();
}
// 根据openid 来删除数据
public void deleteByOpenid(String openid) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_NAME, "openid=?", new String[] { openid });
db.close();
}
// 根据index更新数据库的内容
public void updateByIndex(UsrObj usrObj, int index) {
ContentValues values = new ContentValues();
SQLiteDatabase db = getWritableDatabase();
values.put("index_", usrObj.getIndex());
values.put("openid", usrObj.getOpenID());
values.put("nickname", usrObj.getNickName());
values.put("imagenum", usrObj.getImageNum());
db.update(TABLE_NAME, values, "index_=?",
new String[] { String.valueOf(index) });
db.close();
}
// 根据openid更新数据库的内容
public void updateByOpenid(UsrObj usrObj, String openid) {
ContentValues values = new ContentValues();
SQLiteDatabase db = getWritableDatabase();
values.put("index_", usrObj.getIndex());
values.put("openid", usrObj.getOpenID());
values.put("nickname", usrObj.getNickName());
values.put("imagenum", usrObj.getImageNum());
db.update(TABLE_NAME, values, "openid=?", new String[] { openid });
db.close();
}
// 关闭数据库
public void close() {
if (db != null) {
db.close();
}
}
}
3,sdcard,file
app也可以在sdcard上建个目录保存文件。
// 没有就建,有就返回已有的
// 返回string或null
public static String createPicDir() {
File file = null;
boolean sdExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdExist) {
file = Environment.getExternalStorageDirectory();//sdcard
} else {
file = Environment.getDataDirectory();//data分区
}
Log.d(Tools.LOG_TAG, file.toString());
int i = file.toString().length();
String lastchar = file.toString().substring(i - 1, i);
File destDir = null;
// 根目录加“/”
String rootDir = lastchar.equals("/") ? file.toString() : file
.toString() + "/";
if (sdExist) {
destDir = new File(rootDir + picDirName);
} else {
destDir = new File(rootDir + "data/" + packageName + "/"
+ picDirName);
}
Log.d(Tools.LOG_TAG, destDir.toString());
DeleteFile(destDir);
if (!destDir.exists()) {
if (!destDir.mkdirs()) {
return null;
}
}
return destDir.toString();
}
// 删文件或目录
public static void DeleteFile(File file) {
if (file.exists() == false) {
return;
} else {
if (file.isFile()) {
file.delete();// 删文件
return;
}
if (file.isDirectory()) {
File[] childFile = file.listFiles();
if (childFile == null || childFile.length == 0) {
file.delete();// 删空目录
return;
}
for (File f : childFile) {
DeleteFile(f);
}
file.delete();
}
}
}
4,contentProvider
让不同应用程序数据共享。
a)在当前app中定义contentProvider
b)在manifest.xml中注册contentProvider
c)其他app通过contentResolver和uri获取contentProvider的数据