getAssets()。open - open failed:EROFS(只读文件系统)

时间:2016-01-28 13:58:57

标签: android

我正在尝试将资源文件夹中的文件复制到应用程序的数据目录/内部存储中。

该文件保存在assets文件夹中,文件名为eng.traineddata

以下是我正在使用的代码:

    File file = new File(getFilesDir(), "eng.traineddata");
    if (!file.exists()) {
        try {
            InputStream inputStream = getAssets().open("eng.traineddata");
            OutputStream outputStream = new FileOutputStream("eng.traineddata");
            byte[] buffer = new byte[5120];
            int length = inputStream.read(buffer);
            while (length > 0) {
                outputStream.write(buffer, 0, length);
                length = inputStream.read(buffer);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            Log.e("APP_INITIALIZER", e.getMessage());
        }
    }

但我总是在logcat中收到此消息:eng.traineddata: open failed: EROFS (Read-only file system)

2 个答案:

答案 0 :(得分:0)

像这样创建OutputStream

OutputStream outputStream = new FileOutputStream(file);

答案 1 :(得分:0)

试试这段代码:

    PackageManager m = getPackageManager();
    PackageInfo p = m.getPackageInfo(getPackageName();, 0);
    String dataDir = p.applicationInfo.dataDir;

    OutputStream outputStream = new FileOutputStream(dataDir +"/eng.traineddata");