1. [代码]Intent.ACTION_MEDIA_SCANNER_SCAN_FILE:扫描指定文件
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);
}
2. [代码]“android.intent.action.MEDIA_SCANNER_SCAN_DIR”:扫描指定目录
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);
}
/**
* 这种扫描方式中,由于扫描工作是在MediaScanner服务中进行的,因此不会阻塞当前程序进程。
*当扫描大量媒体文件且实时性要求不高的情况下,适合使用该扫描方式。
*/
3. [代码]通过MediaScanner提供的API接口,扫描媒体文件。
/**
* 这种扫描媒体文件的方式是同步的,扫描工作将会阻塞当前的程序进程。当扫描少量文件,
且要求立即获取扫描结果的情况下,适合使用该扫描方式。
在扫描媒体文件前,程序应该根据终端当前的语言环境正确设置MediaScanner的语言环境设置,
避免产生编解码的错误:*/
MediaScanner scanner = new MediaScanner(ctx);
Locale locale = ctx.getResources().getConfiguration().locale;
String language = locale.getLanguage();
String country = locale.getCountry();
scanner.setLocale(language + \"_\" + country);
4. [代码]扫描全盘(SD卡),相当于重启或者重新加载外置SD卡时扫描外置SD卡
// 开始刷新媒体库
//<4.4
Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.parse("file://"
+ Environment.getExternalStorageDirectory()));
sendBroadcast(intent);