Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking the MediaScanner in just a single directory (android)

I have a problem trying to get MediaScanner to scan just one directory.

My app takes pictures, and saves them to sd-card/DCIM/AppPictures/, and obviously I need to invoke the MediaScanner for them to show up in the gallery app. The following code DOES work for my purpose:

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

But it just seems so wasteful! I don't want to use resources trying to scan the whole SDcard when I know exactly where the files are. I have tried the following too:

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

But this fails to yield any results. Can someone PLEASE let me know the proper way to get this done?

like image 215
SJonesGSO Avatar asked Nov 30 '12 00:11

SJonesGSO


1 Answers

public static void ScanMyFile(String strFilePath) {
    // Tell the media scanner so it is available to the user.
    MediaScannerConnection.scanFile(null, new String[] { strFilePath }, null, 
            new MediaScannerConnection.OnScanCompletedListener() {
        public void onScanCompleted(String path, Uri uri) {

        }
    });
}
like image 94
hB0 Avatar answered Nov 03 '22 22:11

hB0