1. Start the mediascanner service and scan media files:
The program starts the mediascanner service to scan the specified file or directory by sending the following intent:
Intent. action_media_scanner_scan_file: scan a specified file
12345 |
public void scanFileAsync(Context ctx, String filePath) { Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); scanIntent.setData(Uri.fromFile(new File(filePath))); ctx.sendBroadcast(scanIntent); } |
"Android. Intent. Action. media_scanner_scan_dir": scans the specified directory.
123456 |
public static final String ACTION_MEDIA_SCANNER_SCAN_DIR = "android.intent.action.MEDIA_SCANNER_SCAN_DIR"; public void scanDirAsync(Context ctx, String dir) { Intent scanIntent = new Intent(ACTION_MEDIA_SCANNER_SCAN_DIR); scanIntent.setData(Uri.fromFile(new File(dir))); ctx.sendBroadcast(scanIntent); } |
In this scan mode, the current program process is not blocked because the scan is performed in the mediascanner service. This scan method is applicable when scanning a large number of media files with low real-time requirements.