Skip to content

Instantly share code, notes, and snippets.

@0x10FF
Last active April 1, 2023 03:13
Show Gist options
  • Save 0x10FF/3327411 to your computer and use it in GitHub Desktop.
Save 0x10FF/3327411 to your computer and use it in GitHub Desktop.
String imageName = "BGS";
Bitmap sourceBitmap = ((BitmapDrawable) mMediaView.getDrawable()).getBitmap();
boolean imageSaved = false;
if (sourceBitmap != null && !sourceBitmap.isRecycled()) {
File storagePath = new File(Environment.getExternalStorageDirectory() + "/MySpecialLocation/Pictures/");
storagePath.mkdirs();
FileOutputStream out = null;
File imageFile = new File(storagePath, String.format("%s.jpg",imageName));
try {
out = new FileOutputStream(imageFile);
imageSaved = sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
} catch (Exception e) {
Log.e(Constants.LOG_TAG, "Unable to write the image to gallery", e);
} finally {
if (out != null) {
out.flush();
out.close();
}
}
ContentValues values = new ContentValues(3);
values.put(Images.Media.TITLE, imageName);
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put("_data", imageFile.getAbsolutePath()) ;
getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
}
Toast.makeText(EnclosingActivity.this, String.format("Image %ssaved in Media Gallery...", imageSaved ? "" : "NOT "), Toast.LENGTH_SHORT).show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment