安卓系统上的数据库是sqlite,虽然这个数据库是轻量级的,但是储存的东西可不少。
表示理论存储容量为140TB,目前应该没有那么大容量的手机,存储能力太强了。
// 检查数据库是否有效private boolean checkDataBase() {
// SQLiteDatabase checkDB = null
// String myPath = DB_PATH + DB_NAME
File file = new File(DB_PATH, DB_NAME)
return file.exists()
}
public DBCtrl(Context context) {
super()
synchronized (lock) {
this.context = context
DB_PATH = context.getResources().getString(R.string.dbpath)
boolean dbExist = checkDataBase()
if (dbExist) {
// 数据库已存在,do nothing.
} else {
// 创建数据库
try {
File dir = new File(DB_PATH)
if (!dir.exists()) {
dir.mkdirs()
}
File dbf = new File(DB_PATH + DB_NAME)
if (dbf.exists()) {
dbf.delete()
}
// SQLiteDatabase.openOrCreateDatabase(dbf, null)
// 复制asseets中的db文件到DB_PATH下
copyDataBase()
} catch (IOException e) {
throw new Error("数据库创建失败")
}
}
}
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getResources()
.openRawResource(R.raw.db)
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName)
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024]
int length
while ((length = myInput.read(buffer)) >0) {
myOutput.write(buffer, 0, length)
}
// Close the streams
myOutput.flush()
myOutput.close()
myInput.close()
// }
}
我用的这个 构造 helper时候没有数据库就复制了
我觉得你失败的原因不是复制有问题而是数据太大了
android2.3以前不能直接读大于1m的资源的
你把数据库分割成小块复制过来时拼起来吧
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)