Search code examples
androidemulationandroid-gallery

Image is showing in gallery but after restarting emulator


I have implemented an application in which I am downloading an image from a url and storing it on an SD card.
It is showing in the emulator gallery after restarting it. Is it possible to show images in a gallery without restarting emulator?


Solution

  • Approaches:

    #1 If you want immediate results then make use of MediaScannerConnection.

    MediaScannerConnection.scanFile(ctx,new String[] { path.toString() },null,new    MediaScannerConnection.OnScanCompletedListener() {    
    
      public void onScanCompleted(String path, Uri uri) {    
        Log.i("ExternalStorage", "Scanned " + path + ":");    
        Log.i("ExternalStorage", "-> uri=" + uri);   
      }   
    });
    

    #2 If you don't need immediate results(eg. you have created new 100s of images) then instead when you are done downloading all the files you could trigger a scan yourself of the entire SD card by telling the system that the card was just mounted:

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

    Conclusion : If you are writing very small no. of files at a time then make use of #1 otherwise #1 is fast when you are dealing with writing very large no. of files which are to be scanned.