@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory){
File externalFilesDir = Environment.getExternalStorageDirectory();
if(externalFilesDir == null)
return null;
File dbFile = new File(externalFilesDir, SQLiteHelper.DATABASE_NAME);
return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.CREATE_IF_NECESSARY);
}
我使用上面的代码将我的db文件写入外部存储器。所以它保存在/mnt/sdcard/
。当我在Android 2.3.5中尝试它时,它的工作原理。但是使用Android 4它不起作用。但它仍然没有给我任何错误。
答案 0 :(得分:2)
以下是寻找同样事情的其他人的答案。
// ======= This will allow to create the DB in /mnt/sdcard/
// External folder DB access for Android 4+
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode,CursorFactory factory, DatabaseErrorHandler errorHandler) {
File externalFilesDir = Environment.getExternalStorageDirectory();
if(externalFilesDir == null)
return null;
File dbFile = new File(externalFilesDir, SQLiteHelper.DATABASE_NAME);
return super.openOrCreateDatabase(dbFile.getAbsolutePath(), Context.MODE_WORLD_WRITEABLE, null, errorHandler);
}
// External folder DB access for Android 2.3
@Override
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory){
File externalFilesDir = Environment.getExternalStorageDirectory();
if(externalFilesDir == null)
return null;
File dbFile = new File(externalFilesDir, SQLiteHelper.DATABASE_NAME);
return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null, SQLiteDatabase.CREATE_IF_NECESSARY);
}