0

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)
Jerryl15
  • 548
  • 3
  • 10

1 Answers1

2

It seems to me that os is null at the moment. If so, then creating the FileOutputStream threw a FileNotFoundException. You should've posted any logs if you had any.

Also be sure to set the permission READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE.

My advice would be to check if os is not null at the moment, and only then write, because the file opening might fail.

Also I wouldn't rely on emulator over an actual device, because it just emulates the system. If you have any real devices you can test on, use them.

Community
  • 1
  • 1
Sipka
  • 2,291
  • 2
  • 27
  • 30
  • 2
    WRITE_EXTERNAL_STORAGE implicitly allows for **reading** too, you can skip READ_EXTERNAL_STORAGE permission. For the rest, +1 from me. – Phantômaxx Jan 18 '14 at 11:17
  • Thanks for the reply. I will take your advice - check for null then write. My permissions already have READ/WRITE external store. It's just weird that it works on my device, but not on an emulator. Also, there were no logs before then, no FileNotFoundException or anything. An emulator running API 4.3 works, but not API15 – Jerryl15 Jan 18 '14 at 12:01
  • Edit! Just added the null check and it worked! - for some reason without the null check it did not throw a FileNotFoundException, but it did with the null check. – Jerryl15 Jan 18 '14 at 12:07
  • 1
    Issue reolved: In emulator settings, you need to specify the size of external storage ("SD Card") properly. By default, the "external storage" field is empty that probably means there is no such device and EACCES is thrown even if permissions are granted in the manifest. – Jerryl15 Jan 18 '14 at 12:20
  • Citing @Jerryl15 's comment above, reference link http://stackoverflow.com/a/14375593/456814 –  Aug 13 '14 at 19:23