如何获得sqlite的路径,在资源文件夹? [英] How to get the sqlite path, in asset folder?

查看:190
本文介绍了如何获得sqlite的路径,在资源文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里是个初学者。

1.How可我打开一个 sqlite的数据库,它是存储在资产文件夹的Andr​​oid ?我有一个路径用来访问我的应用程序资产文件夹?

1.How can I open a Sqlite database, which is stored in the assets folder in Android? Which path I have to use to access to my app assets folder?

path = "file:///asset_folder/database.dat";
SQLiteDatabase db = null;
db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);

2.How可我安装我的应用程序,或在第一次运行一个文件复制到内置存储器( /数据/数据​​/...),其实我要一个文件夹中的assets`文件夹中复制到内存中的第一次运行。

2.How can I copy a file to the internal memory(/data/data/...) on installing my app or in the first run, actually I want to copy a folder inside the assets` folder into the internal memory on the first run.

推荐答案

我有同样的问题,让我感动的数据库文件到RES /原始文件夹,并访问它像这样:

I was having the same problem, so I moved the db file to the res/raw folder, and accessed it like so:

InputStream inputStream = getBaseContext().getResources().openRawResource(R.raw.mySQLiteFile);

然后我试图将文件移动到文件夹/data/data/com.mydomain.www/databases/,但由于目标路径不存在我会得到一个异常,所以我做了文件(destPath).getParentFile()的mkdir();

从那里,我叫了一份分贝方法到数据库传输到目标。

From there, I called a copy db method to transfer the db to the destination.

public 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();
    }

InputStream为数据库文件的OutputStream是的FileOutputStream(destPath)。

InputStream is the db file, OutputStream is the FileOutputStream(destPath).

这篇关于如何获得sqlite的路径,在资源文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆