Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save images from drawable to internal file storage in Android

I have some 10 images saved in drawable folder. I want to move them to internal file storage.

Is this possible?

I have searched a lot but, I only found links where we can save image to internal file system from given path, but I want to save images from drawable folder to internal file storage in Android and I want to achieve this through code.

Any help will greatly be appreciated.

Thank you.

like image 792
Shrikant Ballal Avatar asked Sep 24 '12 06:09

Shrikant Ballal


1 Answers

Saving image to sdcard from drawble resource:

Say you have an image namely ic_launcher in your drawable. Then get a bitmap object from this image like:

Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);

The path to SD Card can be retrieved using:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();

Then save to sdcard on button click using:

File file = new File(extStorageDirectory, "ic_launcher.PNG");
    outStream = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    outStream.flush();
    outStream.close();

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

like image 138
Shrikant Ballal Avatar answered Oct 20 '22 14:10

Shrikant Ballal