Android 当媒体变更后,通知其他应用重新扫描

 

在媒体文件改变后 发出 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE广播,告知其他应用,媒体文件发生改变。

 

具体代码片段:

 1 File oldFile = new File(oldPath);
 2 File newFile = new File(newPath);
 3 if (oldFile.exists() && !newFile.exists()) {
 4     if (oldFile.renameTo(newFile)) {
 5         
 6         Uri fileUri = Uri.fromFile(newFile);
 7         Intent intent = new Intent();
 8         intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 9         intent.setData(fileUri);
10         sendBroadcast(intent);
11     }
12 }

经过测试上面的代码在三星,联想的手机上不好用

经过试验 将面代码的 6- 10 行换成下面的代码就好用了

sendBroadcast(newIntent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));

 

 

 

 

posted @ 2013-09-25 12:25  轻松  阅读(1850)  评论(0编辑  收藏  举报