如何检查文件是否已被媒体广播接收器扫描

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

我正在向媒体广播接收器发送扫描意图,以便系统 "known" 图像:

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);

等待扫描完成的方法是什么?有听众吗?谢谢

在你的情况下,你需要使用 MediaScannerConnection.scanFile,它会给你一个回调来侦听通过 OnScanCompletedListener 方法完成的扫描操作。

例如

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);
                   }
                });