SoFunction
Updated on 2025-04-10

Solution to save Android pictures to albums and not display them (compatible with Android 10 and higher)

Preface

I wrote a demo, the simple logic is: add a line of text or watermark to a picture and save it to the system album, which is the picture gallery on our mobile phone. There is no problem in editing the picture in the front and adding watermarks. There is a problem in saving it to the system album later: the picture cannot be displayed.

question

Three steps to save system albums before Android 10:

  • Save the picture to your phone
  • Insert the picture into the mobile phone gallery
  • Send broadcast updates

The code is as follows:

public static void savePhotoAlbum(Context context, Bitmap bmp) {
    // Save the picture first    File appDir = new File((), "Boohee");
    if (!()) {
        ();
    }
    String fileName = () + ".jpg";
    File file = new File(appDir, fileName);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        (, 100, fos);
        ();
        ();
    } catch (FileNotFoundException e) {
        ();
    } catch (IOException e) {
        ();
	}
    
    // Next, insert the file into the system library    try {
        ((),
				(), fileName, null);
    } catch (FileNotFoundException e) {
        ();
    }
    // Last notify the gallery update    (new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, ("file://" + path)));
}

Problem: The picture is not displayed, which means it has not been updated to the system gallery.

Careful friends will find that there are two ways to abandon the previous code:

 ((),
				(), fileName, null);
(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, ("file://" + path)));

Solve the problem

The following is the solution to the above problem and is compatible with Android 10 version:

    /**
      * Add watermark and save to system album
      */
    private void imgMerge() {
        new Thread(() -> {
            try {
                Bitmap bitmap = (getResources(), );
                File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "");
                if (!()) {
                    ();
                }
                //Add watermark text location.                Bitmap newBitmap = addTextWatermark(bitmap, "Test demo example");
                //Save to system album                savePhotoAlbum(newBitmap, file);
            } catch (Exception e) {
                ();
            }
        }).start();
    }
    
    /**
      * Save to album
      *
      * @param src Source image
      * @param file file to save to
      */
    private void savePhotoAlbum(Bitmap src, File file) {
        if (isEmptyBitmap(src)) {
            return;
        }
        //Save to file first        OutputStream outputStream;
        try {
            outputStream = new BufferedOutputStream(new FileOutputStream(file));
            (, 100, outputStream);
            if (!()) {
                ();
            }
        } catch (FileNotFoundException e) {
            ();
        }
        //Update the gallery again        if (.SDK_INT >= Build.VERSION_CODES.Q) {
            ContentValues values = new ContentValues();
            (.DISPLAY_NAME, ());
            (.MIME_TYPE, getMimeType(file));
            (.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
            ContentResolver contentResolver = getContentResolver();
            Uri uri = (.EXTERNAL_CONTENT_URI,  values);
            if (uri == null) {
                return;
            }
            try {
                outputStream = (uri);
                FileInputStream fileInputStream = new FileInputStream(file);
                (fileInputStream, outputStream);
                ();
                ();
            } catch (IOException e) {
                ();
            }
        } else {
            (
                    getApplicationContext(),
                    new String[]{()},
                    new String[]{"image/jpeg"},
                    (path, uri) -> {
                        // Scan Completed
                    });
        }
    }

Send broadcasts and insert MediaProvider to add pictures to the album. These two methods have been officially abandoned. Only by using the above method in Android 10 and later versions can you effectively solve the problem of not displaying pictures.

Make a record!

The above is the detailed content of the solution (compatible with Android 10 and higher versions) that does not display Android pictures in the system album. For more information about Android pictures in the system that does not display, please pay attention to my other related articles!