原文地址:
MediaScanner
之所以拿MediaScanner开刀 因为想借用系统的Media Scan 工具
[步骤]
1. 下载并安装Git 过程略 网络上很多
2. 得到该功能的模块地址并使用Git下载之
3.
- AndroidManifest.xml :
- MediaProvider : extends ContentProvider
- MediaScannerCursor.java
- MediaScannerReceiver : extends BroadcastReceiver
- MediaScannerService : extends Service
- MediaThumbRequest
4. 鉴于 并不打算自行实现多媒体扫描 因此 此次重点研究对象:MediaScannerReceiver
5.
- 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.getExternalStorageDirect ory().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.
-
-
7.
- 通过 Intent.ACTION_MEDIA_MOUNTED 进行全扫描
- public
void allScan(){ -
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" -
+ Environment.getExternalStorageDirect ory()))); -
}
-
- public
void fileScan(String fName){ -
Uri data = Uri.parse("file:///"+fName); -
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); -
}
补充: 上述方法是不支持对文件夹的 即:Uri data 必须是 文件的Uri
- 如何扫描某文件夹下所有文件 难道就不可以么? 当然不 借助于Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
我 们可以这么做: 取出该文件夹下的所有子文件 如其是文件且类型符合条件 就取出该文件目录 以
- 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. 鉴于多数人并不关心其原理 仅关系如何使用 故 总结如下:
-
- public
void systemScan(){ -
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" -
+ Environment.getExternalStorageDirect ory()))); -
}
-
- public
void fileScan(String file){ -
Uri data = Uri.parse("file://"+file); -
-
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data)); -
}
- 扫描文件夹 参数:填入该文件夹路径
- 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()); -
} -
} -
} -
}
终于结束了
请顶贴 或者 给意见