How to save Bitmap Image to Gallery in Android Studio
AndroidManifest.xml
<!-- Adding WRITE_EXTERNAL_STORAGE permissions in the AndroidManifest.xml file-->
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
MainActivity.java
package com.learnoset.testproject;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
Bitmap bitmapFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getting Bitmap from res folder. Replace with your own Bitmap aimage
bitmapFile = BitmapFactory.decodeResource(getResources(), R.drawable.image_file);
// saving Bitmap Image to gallery
saveImageToStorage();
}
@SuppressLint("NewApi")
private void saveImageToStorage() {
if (checkPermissions()) {
final String fileName = "your_image_filename.jpg";
OutputStream imageOutStream;
try {
// check if the Api level is greater than 29
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
// save image to Pictures Directory
// You can also choose another directories like Environment.DIRECTORY_DCIM, Environment.DIRECTORY_DOWNLOADS, Environment.DIRECTORY_DOCUMENTS
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
imageOutStream = getContentResolver().openOutputStream(uri);
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File image = new File(imagesDir, fileName);
imageOutStream = new FileOutputStream(image);
}
// showing image to Gallery
showingImageInGallery(fileName);
// compress bitmap to Bitmap.CompressFormat.JPEG format you can also use Bitmap.CompressFormat.PNG format
bitmapFile.compress(Bitmap.CompressFormat.JPEG, 100, imageOutStream);
imageOutStream.close();
} catch (IOException e) {
Toast.makeText(this, "Image could not saved", Toast.LENGTH_SHORT).show();
}
}
}
@SuppressLint("NewApi")
private boolean checkPermissions() {
boolean hasPermissions = false;
// checking if user has granted Memory permissions or not. If not then ask for permissions.
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
hasPermissions = true;
} else {
// request for Memory permissions
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
}
return hasPermissions;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Checking if user has granted memory permissions
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// save bitmap to memory if granted
saveImageToStorage();
}
}
private void showingImageInGallery(String imageFileName) {
// getting image file path from external storage
String imagesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File image = new File(imagesDir, imageFileName);
// converting file path to Uri
Uri imageUri = FileProvider.getUriForFile(
MainActivity.this,
"com.your.application.id.fileprovider",
image);
// Scan Media Image File
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(imageUri);
sendBroadcast(mediaScanIntent);
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Complete Quotes App for Android

How to create custom Update Dialog with Example in Android Studio

Modern Login and Register screen UI design
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.