码迷,mamicode.com
首页 > 移动开发 > 详细

Android问题集锦之三十八:not allowed to send broadcast android.intent.action.MEDIA_MOUNTED

时间:2015-05-16 12:00:33      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:mounted

当我们保存图片后就会发个通知告诉系统让sdcard重新挂载,这样其他程序就会立即找到这张图片。

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
            intent.setData(Uri.fromFile(Environment
                    .getExternalStorageDirectory()));
            sendBroadcast(intent);

但是到了Android4.4就不灵了,Google将MEDIA_MOUNTED的权限提高了,于是就报了一个下面的错误。

W/System.err﹕java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=18338, uid=10087

在stackoverfollow中找到了一个办法,在高版本中使用ACTION_MEDIA_SCANNER_SCAN_FILE去通知系统重新扫描文件。代码如下:

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    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())));
                }
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }

Android问题集锦之三十八:not allowed to send broadcast android.intent.action.MEDIA_MOUNTED

标签:mounted

原文地址:http://blog.csdn.net/lincyang/article/details/45766479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!