Search code examples
androidimagemediabroadcastscanning

How to check if the file has been scanned by media broadcast receiver


I'm sending a scan intent to media broadcast receiver so that that the image would be "known" by the system:

Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);

What is the way to wait until the scan is completed? Any listener? Thanks


Solution

  • IN your case you need to use MediaScannerConnection.scanFile which gives you a callback to listen scan action completed via OnScanCompletedListener method.

    For example

    MediaScannerConnection.scanFile(
                    getApplicationContext(),
                    new String[]{file.getAbsolutePath()},
                    null,
                    new OnScanCompletedListener() {
                       @Override
                       public void onScanCompleted(String path, Uri uri) {
                          Log.v(TAG,
                                "file " + path + " was scanned seccessfully: " + uri);
                       }
                    });