0
我似乎有我的方法來備份當前數據庫的問題。方法如下:NullPointerException/IllegalStateException OutputStream.write()
當使用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)
WRITE_EXTERNAL_STORAGE隱式允許**閱讀**,您可以跳過READ_EXTERNAL_STORAGE權限。其餘的,從我+1。 –
感謝您的回覆。我會採取你的建議 - 檢查空,然後寫。我的權限已經有READ/WRITE外部存儲。這只是奇怪的,它在我的設備上,但不是在仿真器上。此外,在此之前沒有日誌,沒有FileNotFoundException或任何東西。運行API 4.3的仿真程序可以運行,但不能運行API15 – Jerryl15
編輯!剛剛添加了空檢查,它的工作! - 出於某種原因而沒有進行空檢查,它沒有拋出FileNotFoundException,但是它使用了空檢查。 – Jerryl15