从MODE_PRIVATE FileNotFoundException保存并读取文件

时间:2018-05-17 06:48:59

标签: android android-intent android-sdcard android-fileprovider

我在资源文件夹中有一个apk文件。我想从MODE_PRIVATE复制apk文件然后读取它。我写了写和读函数但是无法读取我的文件。我有FileNotFoundException 这是我的功能

 private void writeFilePrivate() {
    AssetManager assetManager = getAssets();
    InputStream in;
    FileOutputStream fos;
    try {
        in = assetManager.open("test.apk");
        fos = openFileOutput("tmp.apk", Context.MODE_PRIVATE);

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            fos.write(buffer, 0, read);
        }
        in.close();
        fos.flush();
        fos.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}



private File readFileFromPrivate() {
    int ch;
    StringBuffer fileContent = new StringBuffer("");
    FileInputStream fis;
    try {
        fis = openFileInput("test.apk");
        try {
            while ((ch = fis.read()) != -1)
                fileContent.append((char) ch);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    String data = new String(fileContent);
    File file = new File(data);
    return file;
}

我在日志中有这个错误

java.io.FileNotFoundException: /data/data/com.mypackage.music/files/test.apk: open failed: ENOENT (No such file or directory)

我做错了什么?

0 个答案:

没有答案