以编程方式将文件从R.raw目录移动到内部存储

时间:2018-07-13 00:51:55

标签: java android file fileinputstream

我在R.raw中有很多文件(具有两个扩展名.ppn和.pv)。我正在尝试将它们移入内部存储。

我尝试这样做:

private static void copyPorcupineConfigFiles(Context context) {
    int[] resIds = {R.raw.alexa, R.raw.americano, R.raw.avocado, R.raw.blueberry,
                    R.raw.bumblebee, R.raw.caterpillar, R.raw.christina, R.raw.dragonfly,
                    R.raw.flamingo, R.raw.francesca, R.raw.grapefruit, R.raw.grasshopper,
                    R.raw.iguana, R.raw.picovoice, R.raw.pineapple, R.raw.porcupine,
                    R.raw.raspberry, R.raw.terminator, R.raw.vancouver, R.raw.params};
    Resources resources = context.getResources();
    for (int resId : resIds) {
        String filename = resources.getResourceEntryName(resId);
        String fileExtension = resId == R.raw.params ? ".pv" : ".ppn";
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new BufferedInputStream(resources.openRawResource(resId),
                    256);
            os = new BufferedOutputStream(context.openFileOutput(filename + fileExtension,
                    Context.MODE_PRIVATE), 256);
            int r;
            while ((r = is.read()) != -1) {
                os.write(r);
            }
            os.flush();
        } catch (IOException e) {
            showErrorToast(context);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                showErrorToast(context);
            }
        }
    }
}

它应该保存在data/0/com.package.name

当我这样做时:

String keywordFilePath = new File(this.getFilesDir(), ".ppn")
            .getAbsolutePath();
String modelFilePath = new File(this.getFilesDir(), "params.pv").getAbsolutePath();

它为我提供了路径:

/data/user/0/com.package.name/files/alexa.ppn

但是,当我使用文件管理器或尝试以编程方式检索文件时,它不存在。

1 个答案:

答案 0 :(得分:0)

您的文件位于data/data/packagename/files/目录中,但对用户不可见。 如果要检查该文件是否在路径中

1。您可以从该目录读取文件

FileOutputStream fileout=openFileOutput("sample.txt", MODE_PRIVATE);
            OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);

            InputStream inputStream =    this.getResources().openRawResource(R.raw.sample);


            BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line).append('\n');
            }
            outputWriter.write(total.toString());


            outputWriter.close();
             System.out.println("file copied");


            //code to read the file contents


            FileInputStream fis = openFileInput("sample.txt");
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader bufferedReader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String line1;
            while ((line1 = bufferedReader.readLine()) != null) {
                sb.append(line1);
            }

             System.out.println("contents:"+sb.toString());
  1. 使用以下命令,使用adb shell将文件复制到sd卡中

转到adb shell并输入 $ cd /data/data/packagename

$ cp data/data/packagename/files/sample.txt /storage/emulated/0/ 您将在/storage/emulated/0/ directory中获得文件  3.如果您想查看该文件,请在代码中使用以下路径,但是您的文件将对用户公开

File file=new File(Environment.getExternalStorageDirectory(),"sample.txt");

有关详细信息,请参阅此link

希望对您有帮助