MediaScanner 研究

转载[url]http://www.iteye.com/topic/697492[/url]

MediaScanner


之所以拿MediaScanner开刀 因为想借用系统的Media Scan 工具 通过Intent直接调用系统的



[步骤]

1. 下载并安装Git 过程略 网络上很多



2. 得到该功能的模块地址并使用Git下载之 地址:git://android.git.kernel.org/platform/packages/providers/MediaProvider.git


3. 分析源代码:

- AndroidManifest.xml : 各组件属性描述文件

- MediaProvider : extends ContentProvider 使用SQLiteDatabase 保存查询数据 action="content://media"

- MediaScannerCursor.java

- MediaScannerReceiver : extends BroadcastReceiver 用于接收指定Broadcast: BOOT_COMPLETED MEDIA_MOUNTED MEDIA_SCANNER_SCAN_FILE 并启动 MediaScannerService 开始扫描

- MediaScannerService : extends Service 执行具体的扫描工作

- MediaThumbRequest



4. 鉴于 并不打算自行实现多媒体扫描 因此 此次重点研究对象: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. 根据以上代码得知:

- 当系统启动完毕 会扫描一次

- 当 ACTION_MEDIA_MOUNTED ACTION_MEDIA_SCANNER_SCAN_FILE 也会扫描


7. 如何调用系统MediaScanner 进行扫描


- 通过 Intent.ACTION_MEDIA_MOUNTED 进行全扫描
public void allScan(){
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}

通过 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE 扫描某个文件
public void fileScan(String fName){
Uri data = Uri.parse("file:///"+fName);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
}

[size=medium][color=red]补充: 上述方法是不支持对文件夹的 即:Uri data 必须是 文件的Uri 如果是文件夹的 其不会起作用的 切记![/color][/size]


- 如何扫描某文件夹下所有文件 难道就不可以么? 当然不 借助于Intent.ACTION_MEDIA_SCANNER_SCAN_FILE

我们可以这么做: 取出该文件夹下的所有子文件 如其是文件且类型符合条件 就取出该文件目录 以 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE方式发送至MediaScannerReceiver 若其为文件夹 则迭代查询之 故实现为:

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.getExternalStorageDirectory())));
}

扫描某个文件 参数:填入该文件的路径
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());
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值