lok*_*usu 1 database backup android
我想创建一个我的数据库的备份,但我手机上没有SDCARD.我想在内部存储中创建备份,但下一个代码失败...
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//storage//sdcard0//MyApp//turnos.db";
String backupDBPath = "turnos.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} mensajeOK("INFO", "Copia creada.");
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
我有权限WRITE_EXTERNAL_STORAGE.
从来没有硬编码路径.
//
不是目录分隔符./
是.
/storage/sdcard0
可能不存在 - 使用Environment.getExternalStorageDirectory()
,或getExternalFilesDir()
getDataDirectory()
不太可能是相关的位置
您现有的数据库,如果您将其置于正常位置,可以通过(例如,您的)getDatabasePath()
可用的方法找到Context
Activity
始终使用例外通过例外记录您的堆栈跟踪Log.e()
,而不是丢弃该信息,以帮助您更好地调试问题
这可能不是您当前方法的所有问题,但它应该让您开始.