【发布时间】:2018-03-28 12:13:46
【问题描述】:
我正在尝试从 android 设备的本机相机应用程序中拍照。为此,我首先创建一个文件,然后附加其 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" />
但没有添加相机权限,我还要求 Android M 或更高版本的运行时权限。 从谷歌文档中可以清楚地看出,如果我们想使用原生相机应用程序为我们的应用程序拍摄照片,我们不需要相机权限,那么为什么它会给我下面说明的这个异常
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}(有附加功能)}来自ProcessRecord{d82d864 6641:com.pencilinapp.pencilin/u0a63}( pid=6641, uid=10063) 撤销权限 android.permission.CAMERA
所以我的问题是,从现在开始我是否必须同时请求相机和存储权限?为什么 Google 文档中没有说明?
我已经研究并检查了许多 stackoverflow 线程,但到目前为止没有一个给出与此问题相关的答案。
【问题讨论】:
-
“从谷歌文档中得到很明显,如果我们想使用原生相机应用程序为我们的应用程序拍摄照片,我们不需要相机权限” - 你确定吗?
-
因为我也给了链接请看这个developer.android.com/training/camera/photobasics.html没有提到添加相机权限
-
Google 说 - 如果您通过调用现有的相机应用程序来使用相机,您的应用程序不需要请求此权限。 developer.android.com/guide/topics/media/camera.html
-
@ShaluTD 并且在我访问过的几乎所有博客中添加了相机应用程序的图像捕捉功能,我看到他们只添加了存储权限,我也这样做了,我的应用程序运行良好一段时间(也许已经从应用设置授予权限,idk ..)但最近当我检查我的图像捕获功能时,它开始给我这个异常
-
我认为你已经将它与使用 Camera API 拍摄图像混淆了。不,我没有使用相机 API,我只是利用 Android 的相机应用程序。我也看过这个文档。是的,当然谷歌说“如果你通过调用现有的相机应用程序来使用相机,你的应用程序不需要请求这个权限”,那么他们为什么现在给出 SecurityException 呢?这就是我想知道的