Last active
April 1, 2023 03:13
-
-
Save 0x10FF/3327411 to your computer and use it in GitHub Desktop.
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
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