在模拟器中第一次尝试将资产中的数据库复制到 Android 的内部数据库时失败

发布于 2024-11-27 08:46:56 字数 280 浏览 2 评论 0原文

我的资产中有一个数据库(1.69 MB),需要复制到内部数据库文件夹。我的问题是,第一次尝试复制数据库,但不复制精确的表。在模拟器文件资源管理器中它的大小变为 3072 (3kB)。

当我使用 sqlite explorer 查看该数据库时,我看不到我的表。数据库中存在的唯一表是 android_metadata 表,其中包含我的区域设置信息的一列。

但是,如果我从数据库中清除该数据库并重新运行应用程序,那么这次它似乎有效。为什么第一次尝试就失败了?我如何确定它不会在真实设备中发生? 这是模拟器的bug吗?

I have a database (1.69 MB) in assets to need copy to internal databases folder. My problem is that for the first try db is copied but not with exact tables. It becomes 3072 (3kB) size in the emulator file explorer.

When I look in that db with sqlite explorer I can't see my tables. The only table exists in db is android_metadata table with one column of my locale info.

But if I clear that db from databases and re-run the application, it seems working for this time. Why does it fail for the first try? How can I be sure it won't happen in real devices?
Is that a bug with the emulator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

智商已欠费 2024-12-04 08:46:56

最后我在 Naresh 的帮助下得出了结论(谢谢)。这里是简短的总结:
第一次运行时,我试图将 1.6mb data.db 文件从资产复制到 /data.../databases 文件夹,该文件夹从未存在过。所以

OutputStream os = new FileOutputStream(destPath);

线路给出了错误。但在那之后,我的 dbhelper 实例调用了 getreaddatabase,它在指定路径下创建了数据库文件夹。 Android放置了一个同名的数据库,但里面没有有用的数据。在我的 copyDatabase 方法中,我将其更新如下:

InputStream is = getBaseContext().getAssets().open(assetsDB);

            //when there is no databases folder fileoutputstream gives error,
            //we have to make sure databases folder exists
            DbAdapter temp = new DbAdapter(getApplicationContext());
            temp.open();        //gets readable database: creates databases folder containing DB_NAME db
            temp.close();       //since we don use this temp, we close

            //this wont give error: because path is now exists (databases folder exists)
            OutputStream os = new FileOutputStream(destPath);

            //copying 1K bytes at a time
            byte[] buff = new byte[1024];

这是我提出的解决方案。顺便说一句,以前的 2.3 版本 sdk 资产数据库大小应该小于 1mb。这是另一个问题,找到了解决方案 这里

finally i've come up the conclusion with the help of Naresh (thank you). Here the short summary:
at the first run i was trying to copy my 1.6mb data.db file from assets to /data.../databases folder which one never existed. So

OutputStream os = new FileOutputStream(destPath);

line gave error. But after that line my dbhelper instance called getreadabledatabase which created the databases folder under specified path. Android put a db with same name but no useful data in it. In my copyDatabase method i updated it as follows:

InputStream is = getBaseContext().getAssets().open(assetsDB);

            //when there is no databases folder fileoutputstream gives error,
            //we have to make sure databases folder exists
            DbAdapter temp = new DbAdapter(getApplicationContext());
            temp.open();        //gets readable database: creates databases folder containing DB_NAME db
            temp.close();       //since we don use this temp, we close

            //this wont give error: because path is now exists (databases folder exists)
            OutputStream os = new FileOutputStream(destPath);

            //copying 1K bytes at a time
            byte[] buff = new byte[1024];

this is the solution i've come up with. By the way, former sdk's of 2.3 assets database size should be less than 1mb. This is anohter issue and found the solution here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文