gpt4 book ai didi

android - 将数据库从 Assets 复制到设备数据文件夹时出现 FileNotFoundException

转载 作者:行者123 更新时间:2023-11-29 14:27:12 25 4
gpt4 key购买 nike

我尝试了很多方法将我的 sqlite 数据库文件复制到我的/data/data/packagename/databases 文件夹,但我仍然卡在 FileNotFoundException 中,由 FileOutputStream 对象触发...这是代码:

public static boolean checkCopyDb(Context c, DbHandler _db) {
try {
String destPath = "/data/data/" + c.getPackageName() + "/databases/db_sociallibraries.db";

File dbFile = new File(destPath);

if(!dbFile.exists()) {
_db.copyDb(c.getAssets().open(DB_NAME), new FileOutputStream(destPath)); // Line 44 - Throws the exception
}
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}

private void copyDb(InputStream inputStream, OutputStream outputStream) throws IOException {

byte[] buffer = new byte[1024];
int length;

while((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}

inputStream.close();
outputStream.close();
}

这是错误:

02-09 01:28:46.384 24222-24222/com.test.michelemadeddu.sociallibraries W/System.err﹕ java.io.FileNotFoundException: /data/data/com.test.michelemadeddu.sociallibraries/databases/db_sociallibraries.db: open failed: ENOENT (No such file or directory) 02-09 01:28:46.384 24222-24222/com.test.michelemadeddu.sociallibraries W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409) 02-09 01:28:46.384 24222-24222/com.test.michelemadeddu.sociallibraries W/System.err﹕ at java.io.FileOutputStream.(FileOutputStream.java:88) 02-09 01:28:46.384 24222-24222/com.test.michelemadeddu.sociallibraries W/System.err﹕ at java.io.FileOutputStream.(FileOutputStream.java:128) 02-09 01:28:46.384 24222-24222/com.test.michelemadeddu.sociallibraries W/System.err﹕ at java.io.FileOutputStream.(FileOutputStream.java:117) 02-09 01:28:46.384 24222-24222/com.test.michelemadeddu.sociallibraries W/System.err﹕ at com.test.michelemadeddu.sociallibraries.DbHandler.checkCopyDb(DbHandler.java:44)

我做错了什么吗?谢谢

最佳答案

为了兼容性,也许您可​​以将内部数据库路径更改为以下

if(android.os.Build.VERSION.SDK_INT >= 17) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}

关于android - 将数据库从 Assets 复制到设备数据文件夹时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28401276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com