Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.int

最近在整理拍照或从相册中取图片功能,忽然有个想法,就是拍照之后的图片立马能在相册中看到。我们都知道相册里的图片数据是在sd卡挂载的时候扫描的,如果要想拍照之后在相册中看到图片是不能的,但是呢,我们可以向系统发出一条sd挂载的广播,欺骗一下系统图库应用让其去扫描sd卡并加载图片。只要在拍照成功之后加如下代码即可:

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

但是,在4.4版本之前的手机上是没啥问题的,4.4版本之后的手机上运行会崩溃,报如下错误:

Caused by:java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=3843, uid=10085
                                                                    at android.os.Parcel.readException(Parcel.java:1546)
                                                                    at android.os.Parcel.readException(Parcel.java:1499)
                                                                    at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2825)
                                                                    at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1339)
                                                                    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:377)
                                                                    at com.cf.takephoto.MainActivity$3.onSuccess(MainActivity.java:122)
                                                                    at com.cf.takephoto.IBuilder.onActivityResult(IBuilder.java:124)
                                                                    at com.cf.takephoto.TakePhoto.onActivityResult(TakePhoto.java:42)
                                                                    at com.cf.takephoto.MainActivity.onActivityResult(MainActivity.java:109)
                           ...

意思是不允许发送(android.intent.action.MEDIA_MOUNTED)广播。经过一番搜索,在stackoverflow得到结论:

It seems that google is trying to prevent this from KITKAT.
Looking at core/rest/AndroidManifest.xml you will notice that broadcast android.intent.action.MEDIA_MOUNTED is protected now. Which means it is a broadcast that only the system can send.

意思是谷歌从4.4版本后保护了一些广播,不允许系统之外的应用发送这些广播,可以AndroidManifest.xml 中看到这条权限被保护了,只有系统能发送。同时给出了两种解决方法:

  • 第一种:

    4.4以上版本用(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)解决,4.4以下用原来的解决办法

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4版本以上
    Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(outputFile); 
    scanIntent.setData(contentUri);
    sendBroadcast(scanIntent);
} else {4.4版本以下
    Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
    sendBroadcast(intent);
}
  • 第二种:

    此方法兼容4.4以下及以上(推荐使用)

MediaScannerConnection.scanFile(this, new String[] {file.getAbsolutePath()},null, null);

thank

stackoverflow

本人水平有限,如有错误,欢迎指正!

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值