0

I use following code to write an image to external storage in android :

File root = android.os.Environment.getExternalStorageDirectory(); 
File dir = new File (root.getAbsolutePath() + "/download");
dir.mkdirs(); 
fileName = "image_2.jpeg";
File file = new File(dir, fileName);

try {
    FileOutputStream outStream = new FileOutputStream(file);
    Bitmap bitmap = BitmapFactory.decodeFile("android.resource://com.mypackage.com/drawable/image_1");
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, outStream);
    outStream.flush();
    outStream.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}   

This code is for reading image_1.jpg from drawable folder, then writing it to download folder in external storage, with image_2.jpeg name. (create download folder in external storage and a file with image_2.jpeg name inside that folder). This code will produce an ((force close)). download folder is created and also the image_2.jpeg is created, but image image_2.jpeg is corrupted.

user3077909
  • 19
  • 1
  • 6
  • "This code will produce an ((force close))" -- use LogCat to examine the Java stack trace associated with your crash. Also, please do not clutter up the user's external storage with random directories. Use `Environment.getExternalStoragePublicDirectory()`, or `getExternalFilesDir()` on `Context`, for more sensible places for your files. – CommonsWare Dec 09 '13 at 23:32

2 Answers2

1

These images in drawable folder can be accessed by BitmapFactory, you can save the bitmap to PNG or JPG.

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
File sd = Environment.getExternalStorageDirectory();
String fileName = "test.png";
File dest = new File(sd, fileName);
try {
    FileOutputStream out;
    out = new FileOutputStream(dest);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush();
    out.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Don't forget to add android.permission.WRITE_EXTERNAL_STORAGE permission.

For other type of images, I think put them into assets folder is a better way.

There is a sample here.

Community
  • 1
  • 1
M D
  • 47,665
  • 9
  • 93
  • 114
0

I did same thing with this code.Try this code:

String[] sampleImagesName = { "image2" };
int[] sampleImages = { R.drawable.image1};
File file;
if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {
    file = new File(Environment.getExternalStorageDirectory()
        .getAbsolutePath() + "/download");
    if (!file.exists()) {
        file.mkdirs();
        SaveSampleToSD();
    }
}
private void SaveSampleToSD() {
        String path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/download";

        for (int i = 0; i < sampleImages.length; i++) {
            try {
                File f = new File(path + "/", sampleImagesName[i] + ".jpg");
                Bitmap bm = BitmapFactory.decodeResource(getResources(),
                        sampleImages[i]);
                FileOutputStream out = new FileOutputStream(f);
                bm.compress(Bitmap.CompressFormat.JPEG, 100, out);

                out.flush();
                out.close();

                Log.e("ImageSaved---------", "saved");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
Zankhna
  • 4,570
  • 9
  • 62
  • 103