Reputation: 1
y project is an application that uses the default android phone camera application to take a picture and save it with a watermark , but the retry / ok selector after capturing the image is annoying and is not user friendly at all . here is my method :
private Uri saveImageToGallery(Bitmap bitmap) {
OutputStream fos;
Uri imageUri = null;
try {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, "captured_image_" + System.currentTimeMillis() + ".jpg");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
fos = getContentResolver().openOutputStream(imageUri);
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File image = new File(imagesDir, "captured_image_" + System.currentTimeMillis() + ".jpg");
fos = new FileOutputStream(image);
imageUri = Uri.fromFile(image);
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(this, "Image saved to gallery", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed to save image", Toast.LENGTH_SHORT).show();
}
return imageUri;
}
Upvotes: 0
Views: 21