23

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Moussa
  • 851
  • 3
  • 10
  • 19
  • What kind of Image? A photo that was just taken? A photo from the gallery? Something else entirely? A little more detail is really necessary. If you posted some relevant code and described what you've tried it would really aid us in providing you help. – MattDavis May 11 '12 at 20:23
  • try this link http://stackoverflow.com/questions/649154/android-bitmap-save-to-location – Rakesh May 11 '12 at 20:31
  • That is a good resource but could someone explain it a little , like where to put the code do i put in OnClick? – Moussa May 11 '12 at 21:58

3 Answers3

45

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

Aden Diamond
  • 301
  • 4
  • 23
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • I want to save the image from a drwable folder not from online! – Moussa May 15 '12 at 20:28
  • @IranianLIon that is even more easy. Say you have an image namely ic_launcher in your drawable then get a bitmap object from this image like: `bm = BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher);` and use the above code, I'm providing it in answer :). – Imran Rana May 16 '12 at 06:22
  • Thanks sorry i was late didnt log on in a while – Moussa May 22 '12 at 22:06
  • Im having trouble with the code, on File file = new File(extStorageDirectory, "ic_launcher.PNG"); outStream = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); All the outputs have an error could you help me the error is "outStream cannot be resolved to a variable" – Moussa May 22 '12 at 22:27
  • @IranianLIon here is the complete sample project: [Save Image](http://www.mediafire.com/download.php?q2x4c3nd55597t4), use it. – Imran Rana May 23 '12 at 09:10
  • Hey imran it works but when i add my set wallpaper button i get an error. could you give me a sample project with a save button and set wallpaper button PLEASE!!!! – Moussa May 24 '12 at 19:10
  • woops my bad i didnt notice i did that, – Moussa May 25 '12 at 20:39
  • Why does it save TWO copies of the image? – Si8 Nov 10 '13 at 15:21
  • yes image will copy in sdcard but why it will heap memory in logcat – Mahesh Nov 10 '14 at 08:55
  • Is there anyway to save gif image?? – Vishal Senjaliya Oct 03 '16 at 10:27
  • Why dimension of png changed after saving? My orignal png in drawable is 180x40, but the image saved to internal storage is 540x120. Exactly 3 time height and width. Any clue? – Rishabh Chandel Jun 17 '17 at 23:08
  • @RishabhChandel [if you get bitmap from a resource, then the bitmap dimension will depend on the phone screen density](https://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size). Also have a look at [this](https://stackoverflow.com/questions/8048603/android-scaling-of-images-to-screen-density) one for scaling. – Imran Rana Jun 18 '17 at 20:42
  • transparent Image becomes black.Please suggest solution. – ajay dhadhal Jun 21 '18 at 11:10
3

I think there are no real solution on that question, the only way to do that is copy and launch from sd_card cache dir like this:

Bitmap bm = BitmapFactory.decodeResource(getResources(), resourceId);
File f = new File(getExternalCacheDir()+"/image.png");
try {
    FileOutputStream outStream = new FileOutputStream(f);
    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    outStream.flush();
    outStream.close();
} catch (Exception e) { throw new RuntimeException(e); }

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(f), "image/png");
startActivity(intent);


// NOT WORKING SOLUTION
// Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + resourceId);
// Intent intent = new Intent();
// intent.setAction(android.content.Intent.ACTION_VIEW);
// intent.setDataAndType(path, "image/png");
// startActivity(intent);
Ian Holing
  • 119
  • 1
  • 3
0

If you use Kotlin, you can do like this:

val mDrawable: Drawable? = baseContext.getDrawable(id)
val mbitmap = (mDrawable as BitmapDrawable).bitmap
val mfile = File(externalCacheDir, "myimage.PNG")
        try {
            val outStream = FileOutputStream(mfile)
            mbitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream)
            outStream.flush()
            outStream.close()
        } catch (e: Exception) {
            throw RuntimeException(e)
        }
Mori
  • 2,653
  • 18
  • 24