我的备份当前数据库的方法似乎有问题。方法如下:
当使用nexus 4,运行4.2.2时,该方法可以正常工作,但是当使用模拟器模拟版本API 15时,会发生错误。发生错误的行是os.write()谢谢。
public void backupDatabase() throws IOException {
File file = context.getDatabasePath(DATABASE_NAME);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
File appDir = new File( Environment.getExternalStorageDirectory() + "/AppName/");
appDir.mkdir();
File outputDB = new File(appDir, "BackupFile.file");
//String outputDB = Environment.getExternalStorageDirectory() +"/AppName/BackupFile.file";
OutputStream os = null;
try {
os = new FileOutputStream(outputDB);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0) {
os.write(buffer, 0, length); //<---Error.... (Only on emulator)
}
// Close the streams
os.flush();
os.close();
fis.close();
Toast.makeText(context, "Data successfully backed up!", Toast.LENGTH_SHORT).show();
}
01-18 18:57:38.077: E/ACRA(1961): PACKAGE_NAME fatal error : Could not execute method of the activity
01-18 18:57:38.077: E/ACRA(1961): java.lang.IllegalStateException: Could not execute method of the activity
01-18 18:57:38.077: E/ACRA(1961): at android.view.View$1.onClick(View.java:3044)
01-18 18:57:38.077: E/ACRA(1961): at android.view.View.performClick(View.java:3511)
01-18 18:57:38.077: E/ACRA(1961): at android.view.View$PerformClick.run(View.java:14105)
01-18 18:57:38.077: E/ACRA(1961): at android.os.Handler.handleCallback(Handler.java:605)
01-18 18:57:38.077: E/ACRA(1961): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 18:57:38.077: E/ACRA(1961): at android.os.Looper.loop(Looper.java:137)
01-18 18:57:38.077: E/ACRA(1961): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-18 18:57:38.077: E/ACRA(1961): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:57:38.077: E/ACRA(1961): at java.lang.reflect.Method.invoke(Method.java:511)
01-18 18:57:38.077: E/ACRA(1961): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 18:57:38.077: E/ACRA(1961): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 18:57:38.077: E/ACRA(1961): at dalvik.system.NativeStart.main(Native Method)
01-18 18:57:38.077: E/ACRA(1961): Caused by: java.lang.reflect.InvocationTargetException
01-18 18:57:38.077: E/ACRA(1961): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:57:38.077: E/ACRA(1961): at java.lang.reflect.Method.invoke(Method.java:511)
01-18 18:57:38.077: E/ACRA(1961): at android.view.View$1.onClick(View.java:3039)
01-18 18:57:38.077: E/ACRA(1961): ... 11 more
01-18 18:57:38.077: E/ACRA(1961): Caused by: java.lang.NullPointerException
01-18 18:57:38.077: E/ACRA(1961): at PACKAGE_NAME.backupDatabase(DbHelper.java:85)
答案 0 :(得分:2)
在我看来,os
目前是空的。如果是,则创建FileOutputStream
投掷FileNotFoundException
。如果你有任何日志,你应该发布任何日志。
另请务必设置permission READ_EXTERNAL_STORAGE
和WRITE_EXTERNAL_STORAGE
。
我的建议是检查os当前是否不是null
,然后再写,因为文件打开可能会失败。
此外,我不会依赖于实际设备上的模拟器,因为它只是模拟系统。如果您有任何可以测试的真实设备,请使用它们。