My app allows a user to save an image to their SD card. But I'm not sure how to make it appear in the gallery until you unmount and remount the SD card. I have googled for a couple of days with this problem but am not sure how to make it appear automatically. I found this link but I'm not sure how to use the class. This is what i use to save the file. At the bottom of the try catch block is where I want to scan the sd card for new media.
FileOutputStream outStream = null;
File file = new File(dirPath, fileName);
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch {
...
}
If anyone could point me in the right direction, I would appreciate.
I've tried plenty of different methods to trigger the MediaScanner, and these are my results.
SendBroadcast
The most simple and naive solution. It consists in executing the following instruction from your code:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
However, this no longer works in KitKat devices, due to a lack of required permissions.
MediaScannerWrapper
As posted here (per @Brian's answer), it consists in wrapping a MediaScannerConnection
instance in order to trigger the scan()
method over a specific directory. This method has proven to be working fine for 4.3 and below, but still no luck with KitKat (4.4+).
FileWalker
One of the many Play Store apps that tries to overcome the MediaStore's lack of commitment to update its database is ReScan SD. It sends a lot of different broadcasts:
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file://" + Environment.getExternalStorageDirectory())));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable/SD")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable/MicroSD")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///mnt/Removable/MicroSD")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///mnt")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///storage")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable")));
and tries to support KitKat by manually triggering the scan()
method over each file of the base directory. Unfortunately, this is both very CPU-intensive and time-consuming, so it is not very recommended.
"The shell way"
The only thing that seem to work with KitKat in some cases is sending the broadcast via adb shell
. So, this snippet allows you to do just that programmatically:
Runtime.getRuntime().exec("am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://" + Environment.getExternalStorageDirectory());
It is more of an hack-ish way of doing it, but at the moment is the best I could come up with.
Bottom line
Each of the above solutions actually works for everything that is not KitKat. That's because, thanks to Justin, a bug has been found and issued to the official Tracker. This means that, until the bug is ironed out, we are left with no true KitKat support.
Which one to use? Among those, I would use the MediaScannerWrapper
solution, together with the shell-ish
approach (the last one).