简体   繁体   English

从资产Android复制SQLite数据库不适用于KitKat 4.4

[英]Android copy SQLite database from asset not work on KitKat 4.4

I red different question here on StackOverflow but mine is a little bit newer. 我在StackOverflow上有一个不同的问题,但我的有点新。

All of these don't work: Question1 Question2 Question3 所有这些都不起作用: 问题1 问题2 问题3

I updated my devices to Android KitKat 4.4 and when I try to copy database with this code: 我将设备更新到Android KitKat 4.4,当我尝试使用以下代码复制数据库时:

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: 我在行获取了一个FileNotFoundException:

OutputStream myOutput = new FileOutputStream(outFileName);

Someone fix this issue on Android KitKat??? 有人在Android KitKat上解决了这个问题??? (other platforms works great) (其他平台很棒)

Thanks for help. 感谢帮助。

Giulio 朱利奥

Full hardcoded 完全硬编码

NEVER HARDCODE PATHS . 从来没有硬编码路径 Use getDatabasePath() to find the proper place for a database. 使用getDatabasePath()查找数据库的正确位置。

other platforms works great 其他平台效果很好

It certainly will crash on Android 4.2+ tablets for secondary accounts. 对于二级账户,它肯定会在Android 4.2+平板电脑上崩溃。 It may crash in other environments as well. 它也可能在其他环境中崩溃。 NEVER HARDCODE PATHS . 从来没有硬编码路径

Note that SQLiteAssetHelper uses getDatabasePath() . 请注意, SQLiteAssetHelper使用getDatabasePath()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM