Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add taken photo to MediaStore

I want to add taken photos to MediaStore so Gallery app can find them (without restarting device). App's min sdk is 9. Any help, blog or documentation appreciated.

like image 699
Ali Behzadian Nejad Avatar asked Feb 17 '14 07:02

Ali Behzadian Nejad


Video Answer


2 Answers

On most devices, all you need to do is wait a little while and the new photos will be detected automatically.

If you want to preform an immediate refresh to the gallery, you need to use the MediaScanner class, It will refresh the gallery - remove deleted photos, add new ones and so on...

public void refreshGallery() {
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    String newPhotoPath = "file:" + image.getAbsolutePath(); // image is the created file image
    File file = new File(newPhotoPath);
    Uri contentUri = Uri.fromFile(file);
    scanIntent.setData(contentUri);
    sendBroadcast(scanIntent);
}

Hope this helped!

like image 182
gilonm Avatar answered Sep 22 '22 17:09

gilonm


sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Insert this line of code after your 'save' code.

This will trigger a media scan and all media files in all folders (except with '.nomedia' files) will be updates & visible in gallery.

Source.

MediaScanner Documentation.

OR

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
        new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
    public void onScanCompleted(String path, Uri uri) {
        Log.i("ExternalStorage", "Scanned " + path + ":");
        Log.i("ExternalStorage", "-> uri=" + uri);
    }
});

Google's Sample Code.

like image 44
Ab5 Avatar answered Sep 19 '22 17:09

Ab5