
我正在尝试从android设备的本机Camera App拍照.为此,我首先创建一个文件,然后附加其URI,以捕获图像并将输出写入该文件.
对于文件提供者,我在AndroidManifest文件中添加了以下内容
<provIDer androID:name="androID.support.v4.content.fileProvIDer" androID:authoritIEs="com.example.myapp.fileprovIDer" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/file_paths"></Meta-data> </provIDer>并且用于捕获图像的Java代码如下
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // Ensure that there's a camera activity to handle the intent if (takePictureIntent.resolveActivity(mActivity.getPackageManager()) != null) { // Create the file where the photo should go file photofile = null; try { String imagename = "image_" + Preferences.getStringForUserID(mActivity); photofile = createImagefile(imagename); mCurrentPhotopath = photofile.getabsolutePath(); } catch (IOException ex) { // Error occurred while creating the file ex.printstacktrace(); } // Continue only if the file was successfully created if (photofile != null) { try { if (androID.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { String authoritIEs = "com.example.myapp.fileprovIDer"; Uri photoURI = fileProvIDer.getUriForfile(mActivity, authoritIEs, photofile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); } else { Uri photoURI = Uri.fromfile(photofile); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); } mActivity.startActivityForResult(takePictureIntent, CAMERA_CODE); } catch (Exception ex) { // Error occurred while creating the file ex.printstacktrace(); Toast.makeText(mActivity, ex.toString(), Toast.LENGTH_LONG).show(); } } } else { Toast.makeText(mActivity, "No Camera App found in device", Toast.LENGTH_LONG).show(); }如Google在AndroID自己的文档中所述:https://developer.android.com/training/camera/photobasics.html
我添加了必要的权限
<uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" /><uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />但是没有添加Camera的权限,我也正在要求AndroID M或更高版本的运行时权限.
从Google文档获得的信息很明显,如果我们想使用本机Camera App来为我们的App捕获照片,则不需要“ Camera Permission”,那么为什么它会给我以下所述的例外情况
java.lang.SecurityException: Permission Denial: starting Intent { act=androID.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.androID.camera/.Camera clip={text/uri-List U:file:///storage/emulated/0/AndroID/data/com.example.myapp/files/Pictures/myapp/image_20130-1383873550.jpg} (has extras) } from ProcessRecord{d82d864 6641:com.pencilinapp.pencilin/u0a63} (pID=6641, uID=10063) with revoked permission androID.permission.CAMERA
所以我的问题是,从现在开始我是否必须同时要求“相机”和“存储”权限?为什么在Google文档中没有说明?
我已经研究并检查了许多stackoverflow线程,但到目前为止,没有一个人给出与该问题有关的答案.
解决方法:
问题是撤销的权限(“具有撤销的权限androID.permission.CAMERA”).通常,您不需要权限,但是如果您请求权限(例如,在您的应用的先前版本中),并且用户撤消了该权限,则您不能使用ACTION_IMAGE_CAPTURE.
总结以上是内存溢出为你收集整理的java-使用android的本机Camera App捕获照片会抛出SecurityException全部内容,希望文章能够帮你解决java-使用android的本机Camera App捕获照片会抛出SecurityException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)