Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I refresh the Gallery after I inserted an Image in android?

Tags:

I've added an inserted in Gallery using android API as following:

Images.Media.insertImage(ctx.getContentResolver(), "scard/test.jpg", "Hello" , "description");

Actually the image that I passed its full path (scard/test.jpg) is already successfully inserted in the DB, but when you open the gallery you can't see it unless you switch off/on the device or Mount/Unmount the external memory.

It there any way to refresh the gallery on demand?

Thanks

Bassel Kh.

like image 520
Bassel Kh Avatar asked Nov 10 '10 13:11

Bassel Kh


People also ask

How do you refresh gallery photos?

Try going to Settings>SD card & phone storage>Unmount Sd card>Mount SD card. That should trigger the media scanner, and your gallery should refresh.

How do I refresh my gallery app?

Try to clear the data from "Media storage" and if you use an SD card, then also clear data from "external storage". Both are found in Apps -> show system apps and look for them. Restart the phone. It shuld be cleared up.


2 Answers

I encountered this problem recently, I tried @Samuh's solution, but not works perfectly until I found the solution from Google's example:

   // 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);         }     }); 

I tried myself and worked flawlessly.

Or, similarly, you might want to take a look at the reference of the MediaScanner Class and someone on StackOverflow asked this question before: Image, saved to sdcard, doesn't appear in Android's Gallery app

like image 102
dumbfingers Avatar answered Oct 01 '22 16:10

dumbfingers


The solution using the Media Scanner (sendBroadcast). But, probably, on sdcards with a lot of pics and data, this operation should reach a high processing cost.

There is another solution, after saving your media file on gallery, you should notify the gallery DB that another file was inserted. That can be done like that:

private void addImageGallery( File file ) {     ContentValues values = new ContentValues();     values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // or image/png     getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } 

This code doesn't save your media file to gallery, only saves the information about a new file in the gallery DB. You should store real file before.

This solution is faster because the gallery will not be fully re-scanned. But is not so trustful because all the information about the file was added manually.

like image 34
Derzu Avatar answered Oct 01 '22 16:10

Derzu