原文地址: http://griffinshi.javaeye.com/blog/697492
MediaScanner
之所以拿MediaScanner開刀 因?yàn)橄虢栌孟到y(tǒng)的Media Scan 工具 通過Intent直接調(diào)用系統(tǒng)的
[步驟]
1. 下載并安裝Git 過程略 網(wǎng)絡(luò)上很多
2. 得到該功能的模塊地址并使用Git下載之 地址:git://android.git.kernel.org/platform/packages/providers/MediaProvider.git
3. 分析源代碼:
- AndroidManifest.xml : 各組件屬性描述文件
- MediaProvider : extends ContentProvider 使用SQLiteDatabase 保存查詢數(shù)據(jù) action="content://media"
- MediaScannerCursor.java
- MediaScannerReceiver : extends BroadcastReceiver 用于接收指定Broadcast: BOOT_COMPLETED MEDIA_MOUNTED MEDIA_SCANNER_SCAN_FILE 并啟動(dòng) MediaScannerService 開始掃描
- MediaScannerService : extends Service 執(zhí)行具體的掃描工作
- MediaThumbRequest
4. 鑒于 并不打算自行實(shí)現(xiàn)多媒體掃描 因此 此次重點(diǎn)研究對(duì)象:MediaScannerReceiver
5. MediaScannerReceiver 代碼
- public class MediaScannerReceiver extends BroadcastReceiver
- {
- private final static String TAG = "MediaScannerReceiver";
-
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- Uri uri = intent.getData();
- String externalStoragePath = Environment.getExternalStorageDirectory().getPath();
-
- if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
- // scan internal storage
- scan(context, MediaProvider.INTERNAL_VOLUME);
- } else {
- if (uri.getScheme().equals("file")) {
- // handle intents related to external storage
- String path = uri.getPath();
- if (action.equals(Intent.ACTION_MEDIA_MOUNTED) &&
- externalStoragePath.equals(path)) {
- scan(context, MediaProvider.EXTERNAL_VOLUME);
- } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&
- path != null && path.startsWith(externalStoragePath + "/")) {
- scanFile(context, path);
- }
- }
- }
- }
-
- private void scan(Context context, String volume) {
- Bundle args = new Bundle();
- args.putString("volume", volume);
- context.startService(
- new Intent(context, MediaScannerService.class).putExtras(args));
- }
-
- private void scanFile(Context context, String path) {
- Bundle args = new Bundle();
- args.putString("filepath", path);
- context.startService(
- new Intent(context, MediaScannerService.class).putExtras(args));
- }
- }
6. 根據(jù)以上代碼得知:
- 當(dāng)系統(tǒng)啟動(dòng)完畢 會(huì)掃描一次
- 當(dāng) ACTION_MEDIA_MOUNTED ACTION_MEDIA_SCANNER_SCAN_FILE 也會(huì)掃描
7. 如何調(diào)用系統(tǒng)MediaScanner 進(jìn)行掃描
- 通過 Intent.ACTION_MEDIA_MOUNTED 進(jìn)行全掃描
- public void allScan(){
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
- + Environment.getExternalStorageDirectory())));
- }
- 通過 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 掃描某個(gè)文件
- public void fileScan(String fName){
- Uri data = Uri.parse("file:///"+fName);
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
- }
補(bǔ)充: 上述方法是不支持對(duì)文件夾的 即:Uri data 必須是 文件的Uri 如果是文件夾的 其不會(huì)起作用的 切記!
- 如何掃描某文件夾下所有文件 難道就不可以么? 當(dāng)然不 借助于Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
我們可以這么做: 取出該文件夾下的所有子文件 如其是文件且類型符合條件 就取出該文件目錄 以 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE方式發(fā)送至MediaScannerReceiver 若其為文件夾 則迭代查詢之 故實(shí)現(xiàn)為:
- public void fileScan(String file){
- Uri data = Uri.parse("file://"+file);
-
- Log.d("TAG","file:"+file);
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
- }
-
- public void folderScan(String path){
- File file = new File(path);
-
- if(file.isDirectory()){
- File[] array = file.listFiles();
-
- for(int i=0;i<array.length;i++){
- File f = array[i];
-
- if(f.isFile()){//FILE TYPE
- String name = f.getName();
-
- if(name.contains(".mp3")){
- fileScan(f.getAbsolutePath());
- }
- }
- else {//FOLDER TYPE
- folderScan(f.getAbsolutePath());
- }
- }
- }
- }
8. 鑒于多數(shù)人并不關(guān)心其原理 僅關(guān)系如何使用 故 總結(jié)如下:
- 掃描全部 我猜測(cè)其在效率方面可能有點(diǎn)副作用
- public void systemScan(){
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
- + Environment.getExternalStorageDirectory())));
- }
- 掃描某個(gè)文件 參數(shù):填入該文件的路徑
- public void fileScan(String file){
- Uri data = Uri.parse("file://"+file);
-
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
- }
- 掃描文件夾 參數(shù):填入該文件夾路徑
- public void fileScan(String file){
- Uri data = Uri.parse("file://"+file);
-
- sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
- }
-
- public void folderScan(String path){
- File file = new File(path);
-
- if(file.isDirectory()){
- File[] array = file.listFiles();
-
- for(int i=0;i<array.length;i++){
- File f = array[i];
-
- if(f.isFile()){//FILE TYPE
- String name = f.getName();
-
- if(name.contains(".mp3")){
- fileScan(f.getAbsolutePath());
- }
- }
- else {//FOLDER TYPE
- folderScan(f.getAbsolutePath());
- }
- }
- }
- }
終于結(jié)束了 看似簡(jiǎn)單的東西 研究起來 還是挺復(fù)雜的!
請(qǐng)頂貼 或者 給意見 謝謝~~~