优先存储SDCard ,无sdcard存在手机内存中!
来源:互联网 发布:线隔电脑编程教学 编辑:程序博客网 时间:2024/05/21 17:28
思路:重写SQLiteOpenHelper getWritableDatabase和getReadableDatabase方法,子类继承实现
package com.c35.mtd.oa.database;
import java.io.File;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
/**
* @title: 移动OA
* @description: sdcard数据库操作类
* @company: 三五互联
* @author sunny
* @version 1.0
* @created on 2011-4-12
*/
public abstract class SQLiteOpenHelper {
private static final String TAG = "SDSQLiteOpenHelper";
private final Context mContext;
private final String mName;
private final CursorFactory mFactory;
private final int mNewVersion;
private SQLiteDatabase mDatabase = null;
private boolean mIsInitializing = false;
public SQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
if (version < 1)
throw new IllegalArgumentException("Version must be >= 1, was "
+ version);
mContext = context;
mName = name;
mFactory = factory;
mNewVersion = version;
}
public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException(
"getWritableDatabase called recursively");
}
// If we have a read-only database open, someone could be using it
// (though they shouldn't), which would cause a lock to be held on
// the file, and our attempts to open the database read-write would
// fail waiting for the file lock. To prevent that, we acquire the
// lock on the read-only database, which shuts out other users.
boolean success = false;
SQLiteDatabase db = null;
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
if(SDCardUtils.isSdCard()){//获得SDCard 数据库
String path = getDatabasePath(mName).getPath();
db = SQLiteDatabase.openOrCreateDatabase(path, mFactory);
Log.i("sunny", "create database in sdcard");
}else{//获得手机数据库
db = mContext.openOrCreateDatabase(mName, 0, mFactory);
}
}
int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try {
mDatabase.close();
} catch (Exception e) {
}
}
mDatabase = db;
} else {
if (db != null)
db.close();
}
}
}
public synchronized SQLiteDatabase getReadableDatabase() {
if (mDatabase != null && mDatabase.isOpen()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException(
"getReadableDatabase called recursively");
}
try {
return getWritableDatabase();
} catch (SQLiteException e) {
if (mName == null)
throw e; // Can't open a temp database read-only!
Log.e(TAG, "Couldn't open " + mName
+ " for writing (will try read-only):", e);
}
SQLiteDatabase db = null;
try {
mIsInitializing = true;
String path = "";
if(SDCardUtils.isSdCard()){//获得SDCard 数据库
path = mContext.getDatabasePath(mName).getPath();
db = SQLiteDatabase.openOrCreateDatabase(path, mFactory);
}else{
path = getDatabasePath(mName).getPath();
db = SQLiteDatabase.openDatabase(path, mFactory,
SQLiteDatabase.OPEN_READWRITE);
}
if (db.getVersion() != mNewVersion) {
throw new SQLiteException(
"Can't upgrade read-only database from version " +
db.getVersion() + " to " + mNewVersion + ": " + path);
}
onOpen(db);
Log.w(TAG, "Opened " + mName + " in read-only mode");
mDatabase = db;
return mDatabase;
} finally {
mIsInitializing = false;
if (db != null && db != mDatabase)
db.close();
}
}
public synchronized void close() {
if (mIsInitializing)
throw new IllegalStateException("Closed during initialization");
if (mDatabase != null && mDatabase.isOpen()) {
mDatabase.close();
mDatabase = null;
}
}
public File getDatabasePath(String name) {
return new File("/sdcard/" + name);
}
public abstract void onCreate(SQLiteDatabase db);
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion);
public void onOpen(SQLiteDatabase db) {
}
}
- 优先存储SDCard ,无sdcard存在手机内存中!
- android代码中判断SDcard是否存在,剩余内存
- 手机内存、sdcard读写操作学习笔记
- sdcard
- sdcard
- android之往手机内存合sdcard卡上存储数据
- 判断sdcard的存在
- 判断sdcard是否存在
- 获取手机中sdCard的权限
- 文件中图片和文档存储到手机和SDCard中
- 把文件存储到sdcard中
- android 手机内存和sdcard内存 的获取
- 【android】查看手机内存状态和sdcard卡内存状态
- 获取手机sdcard大小
- 获得手机sdCard
- SDCard中的存储
- SDCard的数据存储
- android SDCard存储信息
- ASP.NET绘制条形图
- C#连接接MySQL
- js日期在火狐显示为111年的问题
- Nutch的一些分析
- 内核编译篇-------
- 优先存储SDCard ,无sdcard存在手机内存中!
- 数据错误(循环冗余检查);Data error (cyclic redundancy check)
- The elephant on the doorstep: What Wal-Mart’s arrival will mean to D.C.
- 论现场跟客户演示软件产品
- Keep Writing
- 使用 rsync 从windows到Linux同步大量小文件
- VBA读取Excel和CSV
- mvc 路由简单配置(一)
- apache和nginx互切同时都支持fastcgi