Reputation: 53
I got a question how to save a bitmap from my App into the android gallery within a Button? I just want that my bitmap is placed in the gallery that the user can use it to set it as maybe his background. So how is it going?
Upvotes: 0
Views: 1146
Reputation: 150
You can save image in gallery like this.
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
Try like this
The images are displayed in the gallery which are stored on the media store provider
public static void addImageToGallery(final String filePath, final Context context) {
ContentValues values = new ContentValues();
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, filePath);
context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}
Upvotes: 0
Reputation: 766
You modify the following function to suite your needs
private void takeSnapShot(Bitmap bitmap) {
OutputStream outStream = null;
String state = Environment.getExternalStorageState();
String appName = mContext.getString(R.string.app_name);
if (state.equals(Environment.MEDIA_MOUNTED)) {
String sdroot = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = sdroot + "/" + appName;
File dir = new File(path);
if (!dir.exists() && !dir.mkdirs()) {
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
String dstr = sdf.format(new Date());
String filename = path + "/" + dstr + ".jpg";
try {
outStream = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
LogUtils.println("Exception while writing image");
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
try {
if (outStream != null)
outStream.close();
mMediaScanner.scanMediaByName(filename, R.string.snapshot_save);
} catch (IOException e) {
e.printStackTrace();
}
} else {
// Add some parameters to the image that will be stored in the Image ContentProvider
int UNIQUE_BUCKET_ID = 111;
String disp = "anything";
ContentValues values = new ContentValues(7);
values.put(MediaStore.Images.Media.DISPLAY_NAME, disp);
values.put(MediaStore.Images.Media.TITLE, disp);
values.put(MediaStore.Images.Media.DESCRIPTION, appName + disp);
values.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME, appName + " snapshot");
values.put(MediaStore.Images.Media.BUCKET_ID, UNIQUE_BUCKET_ID);
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
// Inserting the image meta data inside the content provider
Uri uri = mContext.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try {
if (uri != null) {
outStream = mContext.getContentResolver().openOutputStream(uri);
if (outStream != null) {
outStream.close();
}
}
} catch (FileNotFoundException e) {
LogUtils.println("Exception while writing image");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Upvotes: 1