vxh.viet
4/26/2016 - 10:49 AM

Refresh MediaStore upon adding new files in Android

Refresh MediaStore upon adding new files in Android

Source: grokkingandroid

Question: How to refresh MediaStore upon adding new files in Android

Answer:

Visit source for additional methods.

Using the static scanFile() method

If you simply need to know when the files have been added, you could use MediaScannerConnection’s static method scanFile() together with a MediaScannerConnection.OnScanCompletedListener.

The static method scanFile() is badly named, as it actually takes an array of paths and thus can be used to add multiple files at once and not just one – but it nevertheless does what we want 

Here’s how to use this method:

private void scanWithPath(Context cont, String[] paths){
        MediaScannerConnection.OnScanCompletedListener callback = new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String path, Uri uri) {
                //this method get trigger for every path in the paths, not when finish scanning the whole paths 
            }
        };

        MediaScannerConnection.scanFile(cont, paths, null, callback);
    }