I seem to be having a problem with my method to backup the current database. The method is as follows:
When using a nexus 4, running 4.2.2 the method works fine, but when using an emulator to emulate version API 15, the error occurs. The line which the error occurs on is os.write() thanks.
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)