Skip to content Skip to sidebar Skip to footer

Saving Photos And Videos Using Android Fileprovider To The Gallery

My application (using SDK less than 24) can take photos and video using the camera. The photos and videos can be viewed in the gallery outside the app. SDK 24 and above requires

Solution 1:

The photo appears in the gallery on a GT-I9300 Galaxy S3 so I know the code works on that device. On a Galaxy 7 the photo does not appear in the gallery until the phone is switched on and off.

After many days of research, the answer is to update the gallery using a media scan on the file. Creating a photo in the PICTURES directory is not enough. Here is a class that scans a single file:

import android.app.Activity;
import android.media.MediaScannerConnection;
import android.net.Uri;

import java.io.File;

classGalleryimplements IGallery {

    privateActivity activity;

    publicGallery(Activity activity) {
        this.activity = activity;
    }

    @OverridepublicvoidscanFile(File file) {
        if(activity == null || file == null) {
            return;
        }

        MediaScannerConnection.scanFile(activity,
                newString[] { file.getAbsolutePath() }, null,
                newMediaScannerConnection.OnScanCompletedListener() {
                    publicvoidonScanCompleted(String path, Uri uri) {
                    }
                });
    }

}

Post a Comment for "Saving Photos And Videos Using Android Fileprovider To The Gallery"