Last active
November 3, 2023 20:36
-
-
Save benny-shotvibe/1e0d745b7bc68a9c3256 to your computer and use it in GitHub Desktop.
Android Save To Gallery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an outdated way to do this... | |
// Inspired by: | |
// http://stackoverflow.com/questions/8560501/android-save-image-into-gallery/8722494#8722494 | |
// https://gist.github.com/samkirton/0242ba81d7ca00b475b9 | |
public static void saveImageToGallery(ContentResolver cr, String imagePath) { | |
String title = "Saved From Glance"; | |
String description = title; | |
ContentValues values = new ContentValues(); | |
values.put(MediaStore.Images.Media.TITLE, title); | |
values.put(MediaStore.Images.Media.DISPLAY_NAME, title); | |
values.put(MediaStore.Images.Media.DESCRIPTION, description); | |
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); | |
// Add the date meta data to ensure the image is added at the front of the gallery | |
long millis = System.currentTimeMillis(); | |
values.put(MediaStore.Images.Media.DATE_ADDED, millis / 1000L); | |
values.put(MediaStore.Images.Media.DATE_MODIFIED, millis / 1000L); | |
values.put(MediaStore.Images.Media.DATE_TAKEN, millis); | |
Uri url = null; | |
try { | |
url = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); | |
if (imagePath != null) { | |
final int BUFFER_SIZE = 1024; | |
FileInputStream fileStream = new FileInputStream(imagePath); | |
try { | |
OutputStream imageOut = cr.openOutputStream(url); | |
try { | |
byte[] buffer = new byte[BUFFER_SIZE]; | |
while (true) { | |
int numBytesRead = fileStream.read(buffer); | |
if (numBytesRead <= 0) { | |
break; | |
} | |
imageOut.write(buffer, 0, numBytesRead); | |
} | |
} finally { | |
imageOut.close(); | |
} | |
} finally { | |
fileStream.close(); | |
} | |
long id = ContentUris.parseId(url); | |
// Wait until MINI_KIND thumbnail is generated. | |
Bitmap miniThumb = MediaStore.Images.Thumbnails.getThumbnail(cr, id, MediaStore.Images.Thumbnails.MINI_KIND, null); | |
// This is for backward compatibility. | |
storeThumbnail(cr, miniThumb, id, 50F, 50F, MediaStore.Images.Thumbnails.MICRO_KIND); | |
} else { | |
cr.delete(url, null, null); | |
} | |
} catch (Exception e) { | |
if (url != null) { | |
cr.delete(url, null, null); | |
} | |
} | |
} | |
/** | |
* A copy of the Android internals StoreThumbnail method, it used with the insertImage to | |
* populate the android.provider.MediaStore.Images.Media#insertImage with all the correct | |
* meta data. The StoreThumbnail method is private so it must be duplicated here. | |
* @see android.provider.MediaStore.Images.Media (StoreThumbnail private method) | |
*/ | |
private static Bitmap storeThumbnail( | |
ContentResolver cr, | |
Bitmap source, | |
long id, | |
float width, | |
float height, | |
int kind) { | |
// create the matrix to scale it | |
Matrix matrix = new Matrix(); | |
float scaleX = width / source.getWidth(); | |
float scaleY = height / source.getHeight(); | |
matrix.setScale(scaleX, scaleY); | |
Bitmap thumb = Bitmap.createBitmap(source, 0, 0, | |
source.getWidth(), | |
source.getHeight(), matrix, | |
true | |
); | |
ContentValues values = new ContentValues(4); | |
values.put(MediaStore.Images.Thumbnails.KIND, kind); | |
values.put(MediaStore.Images.Thumbnails.IMAGE_ID, (int) id); | |
values.put(MediaStore.Images.Thumbnails.HEIGHT, thumb.getHeight()); | |
values.put(MediaStore.Images.Thumbnails.WIDTH, thumb.getWidth()); | |
Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values); | |
try { | |
OutputStream thumbOut = cr.openOutputStream(url); | |
thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut); | |
thumbOut.close(); | |
return thumb; | |
} catch (FileNotFoundException ex) { | |
return null; | |
} catch (IOException ex) { | |
return null; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment