Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
来源:互联网 发布:淘宝上传身份证 编辑:程序博客网 时间:2024/05/22 02:00
当在Android上进行图片的扫描功能开发时一般会使用:sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(“file://”+ Environment.getExternalStorageDirectory()+ picPath)));的广播.
但是在Android4.4及以上版本中,执行上面的代码出现异常:W/ActivityManager( 498): Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=2269, uid=20016
因为Android4.4中限制了系统应用才有权限使用广播通知系统扫描SD卡,所以会抛题目异常。
解决方法有两种方式:
第一种:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//如果是4.4及以上版本 Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri contentUri = Uri.fromFile(mPhotoFile); //out is your output file mediaScanIntent.setData(contentUri); CameraActivity.this.sendBroadcast(mediaScanIntent); } else { sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); }
第二种方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 判断SDK版本是不是4.4或者高于4.4 String[] paths = new String[]{Environment.getExternalStorageDirectory().toString()}; MediaScannerConnection.scanFile(mContext, paths, null, null);} else { final Intent intent; if (f.isDirectory()) { intent = new Intent(Intent.ACTION_MEDIA_MOUNTED); intent.setClassName("com.android.providers.media", "com.android.providers.media.MediaScannerReceiver"); intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); } else { intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(new File(path))); } mContext.sendBroadcast(intent);}
上面两种方式亲测有效,说白了,在高版本中,使用MediaScannerConnection类或者ACTION_MEDIA_SCANNER_SCAN_FILE广播通知系统扫描文件夹,在低版本中使用ACTION_MEDIA_MOUNTED广播通知系统。
我建议使用上面那种方式,上面那种方式简单明了!!
2 0
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播 - Geder
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播 - Geder
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
- Android4.4拒绝发送扫描SD卡的广播
- Android4.4中不能发送SD卡就绪广播
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因
- Android4.2中应用接收开机广播(android.intent.action.BOOT_COMPLETED)失败的原因
- 发送广播,更新sd卡中的内容
- Intent 广播 发送 接收
- Intent发送广播消息
- Android4.2中对SD卡Mount的实现
- Android4.4KK中BOOT_COMPLETED广播问题的分析
- Android4.4 Framework分析——广播的注册(BroadcastReceiver)和发送(sendbroadcast)过程分析
- android 收不到ACTION_MEDIA_MOUNTED 广播
- 左,右,内连接 统计记录数 子查询 聚合函数 查询产品分类
- 黑马程序员----抽象类
- VMware Workstation11.0安装Mac OS X 10.10最完整指南(包含所需所有资源下载)
- Android通过泛型简化findViewById类型转换
- C++那些细节--extern关键字&&全局变量定义问题
- Android4.4中拒绝发送Intent.ACTION_MEDIA_MOUNTED扫描SD卡的广播
- SDUT-2498 AOE网上的关键路径
- Keywords Search(多模式串字符匹配--我的第一道AC自动机)
- 《JavaScript高级程序设计》读书笔记——String方法
- IOS OC NSString基础知识
- 在Mac OS X使用Elasticsearch的基本流程
- Unity3D NGUI从背包中拖出并在场景中生成物体
- 消息中间件原理及JMS简介之二
- 4.Median of Two Sorted Arrays