解决 not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
2016-08-06 16:32
465 查看
背景:我们通过自己的程序更改了媒体数据库,比如说拍摄了照片,或者获取了新的音视频文件,这个时候我们想刷新媒体库,只有在重启手机系统才会去自动扫描SD卡,而我们想通过程序去实现。
在安卓4.4之前,我们可以发送这样一个广播
看一下源代码core/rest/AndroidManifest.xml 你会发现,
请用如下代码代替
其中第二个参数是一个字符串数组,代表打算扫描的文件。
如果要进行版本适配,请使用如下代码
在安卓4.4之前,我们可以发送这样一个广播
android.intent.action.MEDIA_MOUNTED但是,4.4+之后出于安全的考虑,系统禁止我们这个样,会抛异常
not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
看一下源代码core/rest/AndroidManifest.xml 你会发现,
android.intent.action.MEDIA_MOUNTED成为了protected,意味着只有系统才能发广播。
请用如下代码代替
MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() { public void onScanCompleted(String path, Uri uri) { } });
其中第二个参数是一个字符串数组,代表打算扫描的文件。
如果要进行版本适配,请使用如下代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(out); \\out is your output file mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); } else { sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); }
相关文章推荐
- 解决android4.4中 Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- 解决android4.4中 Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED解决方法
- Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED解决方法
- 关于Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Android问题(二)not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- 【Android】not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Android问题集锦之三十八:not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Android问题集锦之三十八:not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
- Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2269,
- 关于 Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from 错误原因
- not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid
- Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_OFF
- Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG
- android中bug——not allowed to send broadcast android.intent.action.phone_state
- 异常问题not allowed to send broadcast android.intent.action.BATTERY_CHANGED可能的一种解决方案
- Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.int
- Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.int
- android java.lang.SecurityException: Permission Denial: not allowed to send broadcast