Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save image to sdcard from drawable resource on Android

Tags:

I'm wondering how to save an image to user's sdcard through a button click. Could some one show me how to do it. The Image is in .png format and it is stored in the drawable directory. I want to program a button to save that image to the user's sdcard.

like image 428
Moussa Avatar asked May 11 '12 20:05

Moussa


1 Answers

The process of saving a file (which is image in your case) is described here: save-file-to-sd-card


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");     FileOutputStream 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.

Here is the modified file for saving from drawable: SaveToSd , a complete sample project: SaveImage

like image 69
Imran Rana Avatar answered Sep 20 '22 12:09

Imran Rana