2

I want to copy a xml file from res/raw folder to the sd card. This question is actually specific to the ODK Collect. But any help would be appreciated. I have looked at Android: How to create a directory on the SD Card and copy files from /res/raw to it? and other similar posts on the web but I was still unable to copy. Maybe, its because I am working on ODK Collect. This is my code for copying the file:

       try {
         InputStream in = getResources().openRawResource(R.raw.problem2);
         OutputStream out = new FileOutputStream(Collect.FORMS_PATH+"/problem2");

                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();

        }           
     catch(IOException e) { }

Thanks in advance.

Community
  • 1
  • 1
Kartikey
  • 21
  • 3

1 Answers1

0

Try this:

private void copyFiles() throws IOException{

        InputStream myInput = m_Context.getAssets().open(FILE_NAME_KEPT_IN_ASSET_FOLDER);
        String outFileName = "/data/data/your.package.name/folder/";
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }
Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139