gpt4 book ai didi

android - 无法从 Android 中的 Assets 文件夹复制数据库

转载 作者:行者123 更新时间:2023-11-29 20:58:48 25 4
gpt4 key购买 nike

我有一个在 SQLite 浏览器中创建的数据库。到目前为止它有 34 个表,我还计划使用 ORMLite 进行数据库插入、删除等操作。我的问题是,由于我是离线创建数据库的,所以我必须将数据库与应用程序一起发送。所以我将数据库保存在 Assets 文件夹中,然后尝试复制数据库。浏览其他 stackoverflow 帖子让我使用 this code .我从我的 MainActivity 调用这个类,这样每当用户进入我的应用程序时,我就可以检查我的数据库是否存在。但事情并没有按预期进行。这是 logcat 输出 link .它显示它无法在 Assets 中找到我的数据库文件。或者我可能是错的。请帮忙!谢谢!

MainActivity.java中的相关代码

AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(this);
SQLiteDatabase db = adb.openDatabase();
if(db.isOpen())
{
System.out.println("Database open, now closing");
db.close();
}

AssetDatabaseOpenHelper.java中的相关代码。

 private static final String DB_NAME = "mydb.db";
public SQLiteDatabase openDatabase() {
File dbFile = context.getDatabasePath(DB_NAME);
System.out.println(dbFile.toString());
//File databasefile = new File(context.getCacheDir(),"databases");
//System.out.println(databasefile.toString());
if (!dbFile.exists()) {
try {
//databasefile.mkdir();
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}

return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}

private void copyDatabase(File dbFile) throws IOException {
InputStream is = context.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(dbFile);

byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}

os.flush();
os.close();
is.close();
}

日志:

11-08 16:24:03.773: E/Trace(5937): error opening trace file: No such file or directory (2)11-08 16:28:32.524: E/Copy Error(7319): Copy may have been aborted11-08 16:30:17.233: E/Trace(7890): error opening trace file: No such file or directory (2)11-08 16:30:18.213: E/Copy Error(7890): Copy may have been aborted11-08 16:32:57.994: E/Trace(8751): error opening trace file: No such file or directory (2)11-08 16:32:58.773: E/Copy Error(8751): Copy may have been aborted11-08 16:42:46.534: E/Trace(11771): error opening trace file: No such file or directory (2)11-08 16:42:47.164: E/AndroidRuntime(11771): FATAL EXCEPTION: main11-08 16:42:47.164: E/AndroidRuntime(11771): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.[packagename ommitted]/com.[packagename ommitted].MainActivity}: java.lang.RuntimeException: Error creating source database11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.ActivityThread.access$600(ActivityThread.java:141)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.os.Handler.dispatchMessage(Handler.java:99)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.os.Looper.loop(Looper.java:137)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.ActivityThread.main(ActivityThread.java:5041)11-08 16:42:47.164: E/AndroidRuntime(11771):    at java.lang.reflect.Method.invokeNative(Native Method)11-08 16:42:47.164: E/AndroidRuntime(11771):    at java.lang.reflect.Method.invoke(Method.java:511)11-08 16:42:47.164: E/AndroidRuntime(11771):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)11-08 16:42:47.164: E/AndroidRuntime(11771):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)11-08 16:42:47.164: E/AndroidRuntime(11771):    at dalvik.system.NativeStart.main(Native Method)11-08 16:42:47.164: E/AndroidRuntime(11771): Caused by: java.lang.RuntimeException: Error creating source database11-08 16:42:47.164: E/AndroidRuntime(11771):    at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:29)11-08 16:42:47.164: E/AndroidRuntime(11771):    at [packagename ommitted].MainActivity.onCreate(MainActivity.java:65)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.Activity.performCreate(Activity.java:5104)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)11-08 16:42:47.164: E/AndroidRuntime(11771):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)11-08 16:42:47.164: E/AndroidRuntime(11771):    ... 11 more11-08 16:42:47.164: E/AndroidRuntime(11771): Caused by: java.io.FileNotFoundException: /data/data/[packagename ommitted]/databases/mydb.db: open failed: ENOENT (No such file or directory)11-08 16:42:47.164: E/AndroidRuntime(11771):    at libcore.io.IoBridge.open(IoBridge.java:416)11-08 16:42:47.164: E/AndroidRuntime(11771):    at java.io.FileOutputStream.(FileOutputStream.java:88)11-08 16:42:47.164: E/AndroidRuntime(11771):    at java.io.FileOutputStream.(FileOutputStream.java:73)11-08 16:42:47.164: E/AndroidRuntime(11771):    at [packagename ommitted].db.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:38)11-08 16:42:47.164: E/AndroidRuntime(11771):    at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:27)11-08 16:42:47.164: E/AndroidRuntime(11771):    ... 15 more11-08 16:42:47.164: E/AndroidRuntime(11771): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)11-08 16:42:47.164: E/AndroidRuntime(11771):    at libcore.io.Posix.open(Native Method)11-08 16:42:47.164: E/AndroidRuntime(11771):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)11-08 16:42:47.164: E/AndroidRuntime(11771):    at libcore.io.IoBridge.open(IoBridge.java:400)11-08 16:42:47.164: E/AndroidRuntime(11771):    ... 19 more11-08 16:45:36.693: E/Trace(12662): error opening trace file: No such file or directory (2)11-08 16:45:37.293: E/AndroidRuntime(12662): FATAL EXCEPTION: main11-08 16:45:37.293: E/AndroidRuntime(12662): java.lang.RuntimeException: Unable to start activity ComponentInfo{[packagename ommitted]/[packagename ommitted].MainActivity}: java.lang.RuntimeException: Error creating source database11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.ActivityThread.access$600(ActivityThread.java:141)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.os.Handler.dispatchMessage(Handler.java:99)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.os.Looper.loop(Looper.java:137)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.ActivityThread.main(ActivityThread.java:5041)11-08 16:45:37.293: E/AndroidRuntime(12662):    at java.lang.reflect.Method.invokeNative(Native Method)11-08 16:45:37.293: E/AndroidRuntime(12662):    at java.lang.reflect.Method.invoke(Method.java:511)11-08 16:45:37.293: E/AndroidRuntime(12662):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)11-08 16:45:37.293: E/AndroidRuntime(12662):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)11-08 16:45:37.293: E/AndroidRuntime(12662):    at dalvik.system.NativeStart.main(Native Method)11-08 16:45:37.293: E/AndroidRuntime(12662): Caused by: java.lang.RuntimeException: Error creating source database11-08 16:45:37.293: E/AndroidRuntime(12662):    at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:29)11-08 16:45:37.293: E/AndroidRuntime(12662):    at [packagename ommitted].MainActivity.onCreate(MainActivity.java:65)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.Activity.performCreate(Activity.java:5104)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)11-08 16:45:37.293: E/AndroidRuntime(12662):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)11-08 16:45:37.293: E/AndroidRuntime(12662):    ... 11 more11-08 16:45:37.293: E/AndroidRuntime(12662): Caused by: java.io.FileNotFoundException: /data/data/[packagename ommitted]/databases/mydb.db: open failed: ENOENT (No such file or directory)11-08 16:45:37.293: E/AndroidRuntime(12662):    at libcore.io.IoBridge.open(IoBridge.java:416)11-08 16:45:37.293: E/AndroidRuntime(12662):    at java.io.FileOutputStream.(FileOutputStream.java:88)11-08 16:45:37.293: E/AndroidRuntime(12662):    at java.io.FileOutputStream.(FileOutputStream.java:73)11-08 16:45:37.293: E/AndroidRuntime(12662):    at [packagename ommitted].db.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:38)11-08 16:45:37.293: E/AndroidRuntime(12662):    at [packagename ommitted].db.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:27)11-08 16:45:37.293: E/AndroidRuntime(12662):    ... 15 more11-08 16:45:37.293: E/AndroidRuntime(12662): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)11-08 16:45:37.293: E/AndroidRuntime(12662):    at libcore.io.Posix.open(Native Method)11-08 16:45:37.293: E/AndroidRuntime(12662):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)11-08 16:45:37.293: E/AndroidRuntime(12662):    at libcore.io.IoBridge.open(IoBridge.java:400)11-08 16:45:37.293: E/AndroidRuntime(12662):    ... 19 more

最佳答案

您应该确保数据库路径存在。

我假设您正在尝试将数据库复制到一个不存在的目录中,因为错误消息是在您在 copyDatabase() 中创建 FileOutputStream 的那一行上的“没有这样的文件或目录”。

关于android - 无法从 Android 中的 Assets 文件夹复制数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26819748/

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