Save bitmap image to specific location of gallery android 10
Try this code :-
private void saveImage(Bitmap bitmap, @NonNull String name) throws IOException {
boolean saved;
OutputStream fos;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = resolver.openOutputStream(imageUri);
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;
File file = new File(imagesDir);
if (!file.exists()) {
file.mkdir();
}
File image = new File(imagesDir, name + ".png");
fos = new FileOutputStream(image);
}
saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
Android save bitmap to image file
1.Check it if you don't request the runtime permission yet: https://developer.android.com/training/permissions/requesting
2.Or if your android is higher than 10:
https://developer.android.com/about/versions/11/privacy/storage#scoped-storage
- After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag.
Then you have to use SAF or MediaStore API to store the bitmap in the "public directory".
SAF:
https://developer.android.com/guide/topics/providers/document-provider
MediaStore API:
https://developer.android.com/reference/android/provider/MediaStore
public void onBtnSavePng(View view) {
try {
String fileName = getCurrentTimeString() + ".jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
values.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/");
values.put(MediaStore.MediaColumns.IS_PENDING, 1);
} else {
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File file = new File(directory, fileName);
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
}
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try (OutputStream output = getContentResolver().openOutputStream(uri)) {
Bitmap bm = textureView.getBitmap();
bm.compress(Bitmap.CompressFormat.JPEG, 100, output);
}
} catch (Exception e) {
Log.d("onBtnSavePng", e.toString()); // java.io.IOException: Operation not permitted
}
}
How to save bitmap in custom gallery?
Try this it may help you (it's in kotlin)
I am currently do this task
private fun saveImage(bitmap: Bitmap) {
var outStream: FileOutputStream? = null
// Write to SD Card
try {
val dir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "ThoughtCast/")
dir.mkdirs()
val fileName = String.format("%s_%d.jpg", "Image", System.currentTimeMillis())
val outFile = File(dir, fileName)
outStream = FileOutputStream(outFile)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
outStream.flush()
outStream.close()
Utils.showSnackBar(binding.rootView, getString(R.string.image_saved))
} catch (e: FileNotFoundException) {
Crashlytics.logException(e)
} catch (e: IOException) {
Crashlytics.logException(e)
} finally {
}
}
Save bitmap to location
try (FileOutputStream out = new FileOutputStream(filename)) {
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (IOException e) {
e.printStackTrace();
}
Save bitmap to android default pictures directory
try this, check if your save directory is exists or create directory first then save bitmap,
String path = Environment.getExternalStorageDirectory().toString()
+ "/Pictures/Keshavarzi/" + "screenshot-" +
System.currentTimeMillis() + ".png";
File imageFile = new File(path);
if(!imageFile.getParentFile().exists()){
imageFile.getParentFile().mkDirs();
}
How to Save bitmap to app folder in android
You can just do this:
try {
FileOutputStream fileOutputStream = context.openFileOutput("Your File Name", Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
It saves your bitmap in the "files" directory in app folder.
Android Studio - Save bitmap to gallery is incorrect
Try this one, maybe it helps you.
private void saveImage(Bitmap bitmatFile, String image_name) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root);
myDir.mkdirs();
String fname = "Image-" + image_name+ ".jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
Log.i("LOAD", root + fname);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmatFile.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Related Topics
Getextractedtext on Inactive Inputconnection Warning on Android
How to Allow Users to Check for the Latest App Version from Inside the App
Send Sms Until It Is Successful
Onclick Event Is Not Triggering | Android
Android Httppost: How to Get the Result
Dx Bad Class File Magic (Cafebabe) or Version (0033.0000) with Adk14
How to Get Difference Between Two Dates in Android, Tried Every Thing and Post
How to Give an Imageview Click Effect Like a Button on Android
Getting Jsoup to Support Dynamically Generated HTML by JavaScript
How to Get String Array from Arrays.Xml File
Android Audio Fft to Retrieve Specific Frequency Magnitude Using Audiorecord
Java Doesn't Work with Regex \S, Says: Invalid Escape Sequence
Deprecated Managedquery() Issue
Android: Prompt User to Save Changes When Back Button Is Pressed
Android Studio Could Not Initialize Class Org.Codehaus.Groovy.Runtime.Invokerhelper