0

我有这个代码:在模拟器中工作但在设备中失败。我想将存储在资产中的数据库复制到数据/数据/....当加载应用程序出现“错误文件”(FileNotFoundException e)-> 错误

public void copiarDB() {
        try {
            String destPath = "/data/data/" + getPackageName()
                    + "/databases/MiBD";
            File f = new File(destPath);
            if (!f.exists()) {
                CopyDB(getBaseContext().getAssets().open("datosejer"),
                        new FileOutputStream(destPath));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Error File", Toast.LENGTH_SHORT)
                    .show();
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "Error IO", Toast.LENGTH_SHORT)
                    .show();
        }
        AdaptadorBD db = new AdaptadorBD(this);
        // ---obtener todos los contactos---
        db.abrir();
        Cursor c = db.obtenerTodosLosDatosEjercicio();
        if (c.moveToFirst()) {
            do {
                // DisplayContact(c);
            } while (c.moveToNext());
        }
        db.cerrar();
    }

    public void CopyDB(InputStream inputStream, OutputStream outputStream)
            throws IOException {
        // ---copy 1K bytes at a time---
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
        Toast.makeText(MainActivity.this, "Se ha copiado la Base de datos",
                Toast.LENGTH_SHORT).show();
    }

我在模拟器中运行并且工作完美,但在我的设备中无法正常工作,模拟器的操作系统是 4.3,我的设备的操作系统是 4.0.4(rooted)、4.0.4 和 4.3。

一行数据库是:

14 11107 Ovalo con mancuernas 11 Hombros 3 12 10 No hay datos Utiliza inicialmente las mancuernas con menor peso que encuentres en tu gimnasio para que aprendas el movimiento ya dominar el peso。Trabajarás la región anterior del hombro。img11107_1.png img11107_2.png img11107_3.png

更新:

我将解决方案放在下一条评论中。

4

1 回答 1

0

最后我找到了解决方案:

public void copyDB() {
    String destPath = "/data/data/" + getPackageName() + "/databases/MiBD";
    File f2 = new File(destPath);
    try {
        if (!f2.exists()) {
            //create a empty file
            InputStream iStream = getBaseContext().getAssets().open(
                    "datosejer");
            AdaptadorBD db = new AdaptadorBD(getApplicationContext());
            db.open();
            db.close();
            OutputStream oStream = new FileOutputStream(destPath);
            //open a file that was created and write data.
            db.open();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = iStream.read(buffer)) > 0) {
                oStream.write(buffer, 0, length);
            }
            iStream.close();
            oStream.close();
            db.close();
        }
    } catch (IOException e1) {
        e1.printStackTrace();
        Toast.makeText(MainActivity.this, "Error.",
                Toast.LENGTH_SHORT).show();
    }
}

现在这项工作在设备中

于 2013-08-07T14:36:23.293 回答