Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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
使用Android 4.4复制数据库_Android_Database_Android 4.4 Kitkat - Fatal编程技术网

使用Android 4.4复制数据库

使用Android 4.4复制数据库,android,database,android-4.4-kitkat,Android,Database,Android 4.4 Kitkat,当我想将数据库从资产复制到设备时出现问题。我总是得到一个FileNotFoundException。同时终止并重新启动“不工作”。在调试器中,变量的内容似乎正常 我的代码: private void copyDatabase() throws IOException { if (DEBUG) Log.d(TAG, "copyDatabase"); String DB_PATH = myContext.getDatabasePath(DATABASE_NAME).toString(

当我想将数据库从资产复制到设备时出现问题。我总是得到一个FileNotFoundException。同时终止并重新启动“不工作”。在调试器中,变量的内容似乎正常

我的代码:

private void copyDatabase() throws IOException {
    if (DEBUG) Log.d(TAG, "copyDatabase");
    String DB_PATH = myContext.getDatabasePath(DATABASE_NAME).toString();

    AssetManager assetManager = myContext.getResources().getAssets();
    InputStream inputStream = null;

    try {
        inputStream = assetManager.open(DATABASE_NAME);
    } catch (IOException ex) {
        Log.d(TAG, "InputStream Exception");
    }

    if (inputStream != null) {
        OutputStream outputStream = new FileOutputStream(DB_PATH);

        byte[] buffer = new byte[1024];
        int length;

        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }

        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }
}
行OutputStream OutputStream=newfileoutputstreamdb\u路径;引发异常

数据库路径:/data/data/com.wiget.autokatalog/databases/autokatalog.db3 原因ErrnoException id=830031910480详细信息 /data/data/com.wiget.autokatalog/databases/autokatalog.db3:打开 失败:eNONT没有这样的文件或目录id=830031912168

我认为该文件将使用这一行创建

是否存在权限问题,例如AndroidManifest中缺少某些内容

谢谢你的帮助


/如果在此之前未创建任何数据库,则可能不存在Andre

/data/data/com.wiget.autokatalog/databases/。请尝试以下操作:

    public File copyFromAssetsTo(String fromFile, String toPath) {
    AssetManager assetManager = context.getAssets();

    try {
        String[] arr = toPath.split("/");
        String fileName = arr[arr.length - 1];
        String fileDirPath = toPath.replaceAll(fileName, "");
        File fileDir = new File(fileDirPath);

        if(!fileDir.exists()) {
            fileDir.mkdirs();
        }

        File file = new File(fileDir, fileName);
        InputStream in = assetManager.open(fromFile);
        OutputStream out = new FileOutputStream(file);
        copyFile(in, out);
        out.close();
        return file;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

谢谢,创建目录有助于解决问题。很抱歉,我没有足够的声誉进行投票:-