Android(安卓)当媒体变更后,通知其他应用重新扫描
16lz
2021-01-24
在媒体文件改变后 发出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())));
更多相关文章
- More than one file was found with OS independent path 'lib/a
- Didn't find class "androidx.core.content.FileProvider" on pa
- Android(安卓)国际化语言设置,简单粗暴。
- 转发:bitmap 设置图片尺寸,避免 内存溢出 OutOfMemoryError的优化
- android注入代码之注入类方法
- AndroidStudio-Eat-Guide—— 1.下载安装配置
- 推荐一系列优秀的Android开发源码
- Android下高斯模糊的算法和demo
- Android(安卓)volley封装实践其二