您的位置:首页 > 移动开发 > Android开发

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

2013-09-25 12:25 309 查看
在媒体文件改变后 发出 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE广播,告知其他应用,媒体文件发生改变。

具体代码片段:

File oldFile = new File(oldPath);
File newFile = new File(newPath);
if (oldFile.exists() && !newFile.exists()) {
if (oldFile.renameTo(newFile)) {

Uri fileUri = Uri.fromFile(newFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(fileUri);
sendBroadcast(intent);
}
}


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

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

sendBroadcast(newIntent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐