Image not showing up in gallery

2019-08-30 11:10发布

问题:

My app is able to take a photo and stores it into a specified folder. Using ES File explorer, I'm able to confirm that the image has been stored in sd card and the specified folder.

However, my gallery is not showing this album immediately. It will need to take quite some time. Is there any ways that i can make the detection immediate using codes?

The following broadcasting codes did not seem to work. My gallery doesnt immediately do a refresh to retrieve the newly created album and image.

Kindly assist. Thanks!

if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".png");

             Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
             Uri contentUri = Uri.fromFile(mediaFile);
             mediaScanIntent.setData(contentUri);
             v.getContext().sendBroadcast(mediaScanIntent);

        }

回答1:

It might take some time till it finished the scanning.

If you wish, you can at least be notified when it finished:

MediaScannerConnection.scanFile(
      getApplicationContext(), 
      new String[]{file.getAbsolutePath()}, 
      null, 
      new OnScanCompletedListener() {
         @Override
         public void onScanCompleted(String path, Uri uri) {
            // file was scanned
         }
      });

for more information read here.