Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Created April 26, 2016 10:49
Show Gist options
  • Save vxhviet/a4b7fca0f3287857a0851862115edefe to your computer and use it in GitHub Desktop.
Save vxhviet/a4b7fca0f3287857a0851862115edefe to your computer and use it in GitHub Desktop.
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);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment