簡體   English   中英

以編程方式將文件從R.raw目錄移動到內部存儲

[英]Moving files from R.raw directory to internal storage programmatically

我在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

但是,當我使用文件管理器或嘗試以編程方式檢索文件時,它不存在。

您的文件位於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獲取文件。如果要查看該文件,請在以下路徑中使用您的代碼,但您的文件將對用戶公開

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

有關詳細信息,請參閱此鏈接

希望對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM