3

I red different question here on StackOverflow but mine is a little bit newer.

All of these don't work: Question1 Question2 Question3

I updated my devices to Android KitKat 4.4 and when I try to copy database with this code:

private void copyDataBase() throws IOException {
    InputStream myInput = context.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();
}

I obtain a FileNotFoundException at line:

OutputStream myOutput = new FileOutputStream(outFileName);

Someone fix this issue on Android KitKat??? (other platforms works great)

Thanks for help.

Giulio

Community
  • 1
  • 1
Giulio Bider
  • 1,386
  • 2
  • 10
  • 20

1 Answers1

4

Full hardcoded

NEVER HARDCODE PATHS. Use getDatabasePath() to find the proper place for a database.

other platforms works great

It certainly will crash on Android 4.2+ tablets for secondary accounts. It may crash in other environments as well. NEVER HARDCODE PATHS.

Note that SQLiteAssetHelper uses getDatabasePath().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • It's a really strange behavior. I installed app and at first launch it crashes with FileNotFoundException. Then I terminate app, relaunch and all works. I'm not crazy! Anyway thanks for advice, I will replace getDatabasePath() instead hardcoded path. Thanks! :) – Giulio Bider Nov 19 '13 at 13:30
  • @GiulioBider: It is possible that you need to create the directory yourself; I do not see where you are doing that in your code. – CommonsWare Nov 19 '13 at 13:32
  • Replaced getDatabasePath() instead of hardcoding path. I use code above inside constructor of SQLiteHelper subclass. First launch doesn't work, if I terminate app and relaunch all works great. – Giulio Bider Nov 20 '13 at 10:57
  • hi commonsware! your answer solved my query too! but when i obfuscate the build i get same issue! does Obfuscation has to do something with the issue – Neji Nov 28 '13 at 14:20