Android调用相机保存图片到相册

来源:互联网 发布:软件开发工程师职称 编辑:程序博客网 时间:2024/05/16 16:08

项目中需要使用相机拍照然后保存到相册中,然而保存后却发现相册中并没有这张图片,但是从相册的文件夹中可以看到。这说明相册没有及时刷新才会这样的。搜寻前辈的答案发现有两种解决方案,第一种是发送广播然后通知相册刷新,第二种是使用MediaScannerConnection 类来刷新。但在Android 4.4以上版本中第一种方法不行,会抛出安全异常。

  1. 发送广播会抛出异常:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + context.getExternalFilesDir(null))));06-05 17:34:41.940: E/AndroidRuntime(15410): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED
  1. 使用MediaScannerConnection 来刷新
MediaScannerConnection.scanFile(getApplicationContext(),    new String[] { newScreenFile.getAbsolutePath() }, null, null);

不过注意的是上面的代码需放在合适的位置,否则无效。

另外附上万能的Stack Overflow大神们回复的。

这里写图片描述 http://stackoverflow.com/questions/24072489/java-lang-securityexception-permission-denial-not-allowed-to-send-broadcast-an

0 0
原创粉丝点击