I have an ImageView in my app, and i also have a button that says "click here to save picture". What the button is supposed to do should be self explanatory. Its supposed to save the ImageView in a new folder in their SD card. I have looked everywhere as to what piece of code can accomplish this. I have found some that are similar, but they wont work for me. If anyone could please help me understand what i need to do, or if they know what code will work, It would be of great help to me. Thanks!
2 Answers
Here's a workflow you could use to get the job done:
- Upon clicking the button, you fire off an onClick(View) (via XML would be OK)
- onClick(View) would have to grab a Bitmap off the ImageView
- Furthermore, once you have the Bitmap, you'll have to save it to the SDCARD
Hope that helps out a bit!

- 1
- 1

- 6,031
- 1
- 26
- 41
-
i think i was able to figure it out with your links ninetwozero, and im not getting any more errors, but im not sure if they save succesfully. i dont know how i can check because i dont have an android phone, only the computer emulator, and i tried but couldnt check. thanks for the help! – Philip Jul 14 '12 at 14:31
Basically, you'll want to get yourself a resulting Bitmap
from the image data, and then write that Bitmap
out to a file. Depending on what level of access you have to the content that was placed in the ImageView
...
If you have the raw Bitmap
, you're done.
If the image was in a resource, use BitmapFactory.decodeResource()
to obtain one
If you have a Drawable
of the content, draw it into a new Bitmap
like so:
Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
If you don't have any access to the image content, use ImageView.getDrawingCache()
to get a Bitmap
of the visible contents. You need to call setDrawingCacheEnabled()
for this to work.
Once you have a Bitmap
, just use the compress()
method to write that image out to a file as a JPG or PNG.

- 62,780
- 12
- 127
- 139