简体   繁体   中英

Moving files from R.raw directory to internal storage programmatically

I have a lot of files (of two extensions, .ppn and .pv) in R.raw . I'm trying moving them into internal storage.

I've tried doing this:

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);
            }
        }
    }
}

It should save in data/0/com.package.name

When I do:

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

It gives me the path:

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

However, when I look with File Manager or try retrieving the file programatically, it's not there.

Your files are there in data/data/packagename/files/ directory but its invisible to user. if you want to check whether that file is at the path or not then

1.You can read file from that directory

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. copy the file using adb shell to your sd card using the commands below

go to adb shell and type $ cd /data/data/packagename

$ cp data/data/packagename/files/sample.txt /storage/emulated/0/ and you will get your file in /storage/emulated/0/ directory 3. If you want to see that file then use the following path in your code but your file will become public to user

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

for details refer this link

hope it helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM