Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a bitmap to disk/gallery in Android 4.3

Tags:

android

I've been using the way the system saves screenshots to save my bitmaps to the disk and gallery. This works in Android 4.2 and before but not in Android 4.3.

Relevant code :

Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream out = resolver.openOutputStream(uri);

Full code here.

In 4.3 (new Nexus 7) however, I get a FileNotFoundException on the second line. I couldn't see any changes in 4.3 relevant to this on the website.

So what is the right way to save an image to the disk and gallery?

Verified :

  • storage is mounted with this method
  • imageUri is not null (usually something like "content://media/external/images/media/2034")
  • manifest has permission android.permission.WRITE_EXTERNAL_STORAGE
like image 498
f2prateek Avatar asked Jul 28 '13 22:07

f2prateek


1 Answers

This is the way I save bitmaps to the Gallery:

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri; //instantiate Uri with location of image
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
like image 130
Phil Avatar answered Oct 03 '22 09:10

Phil