0

A way to save a non text file to /data/files folder. If file resource is from assets folder.

eunique0216
  • 875
  • 1
  • 16
  • 28

1 Answers1

1

this should do it.

private void writeToSDCard() {
    try {
        File root = Environment.getExternalStorageDirectory();

        if (root.canWrite()){
            InputStream from = myContext.getResources().openRawResource(rID);
            File dir = new java.io.File (root, "pdf");
            dir.mkdir();
            File writeTo = new File(root, "pdf/" + attachmentName);
            FileOutputStream  to = new FileOutputStream(writeTo);

            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead); // write
            to.close();             
            from.close();
        } else {
            Log.d(TAG, "Unable to access SD card.");
        }
    } catch (Exception e) {
        Log.d(TAG, "writeToSDCard: " + e.getMessage());
    }
}       
}
nickfox
  • 2,835
  • 1
  • 26
  • 31
  • Cool. I also found this: http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard ... Almost the same. I modified the code so that it would save to /data/data/[PACKAGE_NAME]/files and it works. Thanks so much. – eunique0216 Jul 04 '11 at 10:01