获取资产文件夹中的 SQLite 路径
我正在开发一个应用程序,在这里遇到了两个问题:
如何打开存储在资产文件夹中的 SQLite 数据库?我应该使用什么路径来访问应用程序中的资产文件夹?这是我到目前为止所拥有的:
path = "file:///asset_folder/database.dat"; SQLite数据库 db = null; db = SQLiteDatabase.openDatabase(路径, null, SQLiteDatabase.OPEN_READONLY);
如何在应用程序安装期间或首次运行期间将文件复制到内部存储器 (
/data/data/...
)?我想在第一次运行时将assets文件夹中的一个文件夹复制到内存中。
任何建议将不胜感激。
I'm developing an application and I've run into two problems here:
How can I open an SQLite database which is stored in the assets folder? What path do I use to access the assets folder in my application? Here's what I have so far:
path = "file:///asset_folder/database.dat"; SQLiteDatabase db = null; db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
How can I copy a file to the internal memory (
/data/data/...
) during the installation of my application or during the first run? I want to copy a folder inside the assets folder into the internal memory during the first run.
Any suggestions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,所以我将 db 文件移动到 res/raw 文件夹,并像这样访问它:
然后我尝试将文件移动到 /data/data/com.mydomain.www/databases/ 文件夹中,但我会得到一个异常,因为目标路径不存在,所以我做了
File(destPath).getParentFile().mkdir();
从那里,我调用了一个复制数据库方法来传输db 到目的地。
InputStream是db文件,OutputStream是FileOutputStream(destPath)。
I was having the same problem, so I moved the db file to the res/raw folder, and accessed it like so:
Then I tried to move the file into the /data/data/com.mydomain.www/databases/ folder, but I would get an exception because the destination path didn't exist, so I did
File(destPath).getParentFile().mkdir();
From there, I called a copy db method to transfer the db to the destination.
InputStream is the db file, OutputStream is the FileOutputStream(destPath).
从这篇帖子我发现此教程。它告诉您如何处理资产文件夹中的数据库。这至少回答了你的第一个问题。我不太确定你的第二个。
From this post i found this tutorial. It tells you how to deal with databases in your assets folder. That at least answers your first question. I'm not too sure about your second.
将数据库从资产文件夹复制到应用程序的 data/data/database 文件夹。
使用下面的代码。
Copy database from assets folder to data/data/database folder of your application.
Use Below code.