Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android从资产文件夹复制数据库_Java_Android_Sqlite - Fatal编程技术网

Java android从资产文件夹复制数据库

Java android从资产文件夹复制数据库,java,android,sqlite,Java,Android,Sqlite,我无法处理从资产文件夹到数据库文件夹的数据库。当用户启动应用程序时,我检查数据库是否不存在,如果为真,我复制数据库 我的代码在哪里 private void CopyDatabaseIfNotExists() { dbName = "quizdb.db"; File f = getDatabasePath(dbName); if (f.exists()) return; System.out.p

我无法处理从资产文件夹到数据库文件夹的数据库。当用户启动应用程序时,我检查数据库是否不存在,如果为真,我复制数据库

我的代码在哪里

    private void CopyDatabaseIfNotExists() {

        dbName = "quizdb.db";

        File f = getDatabasePath(dbName);
        if (f.exists())
            return;

        System.out.println("db missing");

        try {

            InputStream mInputStream = getAssets().open(dbName);

            OutputStream mOutputStream = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = mInputStream.read(buffer)) > 0) {
                mOutputStream.write(buffer, 0, length);
            }
            mOutputStream.flush();
            mOutputStream.close();
            mInputStream.close();

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
我得到了这个错误:

06-15 12:29:04.882  25037-25037/com.ex.example W/System.err﹕ java.io.FileNotFoundException: /data/data/com.ex.example/databases/quizdb.db: open failed: ENOENT (No such file or directory)

我已经试过寻找解决方案,但找不到。有人能帮我吗?谢谢你,对不起我的英语。

试试这个……它对我有用。。!!并且您确定已将数据库文件放入资产文件夹中

 private void copyDataBase() throws IOException {
    InputStream is = myContext.getAssets().open(DB_NAME);
    // Log.v("Tag assets",is.toString());
    String outFileName = DB_PATH + DB_NAME;
    OutputStream out = new FileOutputStream(outFileName);
    Log.v("Tag assets", out.toString());
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0) {
        // Log.v("Tag",out.toString());
        out.write(buffer, 0, length);
        // Log.v("Tag",out.toString());
    }
    // Log.v("Tag","Database created");
    is.close();
    out.flush();
    out.close();      

}

FileOutputStream
在文件不存在且无法创建时引发此异常。我以前遇到过这个问题,并通过在
OutputStream moutpstream=newfileoutputstream(f)之前调用
Context
对象(或
SQLiteDatabase
类)的
openOrCreateDatabase
方法设法解决了这个问题

请不要为此滚动您自己的解决方案。its路径,您必须在设备中存储数据库文件,如私有静态字符串DB_path=“/data/data/com.example.yourapplication/databases/”;我敢肯定Android Studio会对你大喊大叫不要硬编码
/data/data/
好的,谢谢你的帮助,你说的100%正确,我只是在
InputStream
前面加了这两行,现在这行很好
Context=getApplicationContext();openOrCreateDatabase(“quizdb”,context.MODE_PRIVATE,null)
要完成,你能说我调用的
openOrCreateDatabase
是否正确吗?你调用它的方式在我看来很好。