即使在清单文件中声明了适当的权限,也无法访问相机
Posted
技术标签:
【中文标题】即使在清单文件中声明了适当的权限,也无法访问相机【英文标题】:Not able to access camera even though proper permissions are declared in manifest file 【发布时间】:2018-07-03 10:57:51 【问题描述】:我编写了一个代码来访问相机以拍摄个人资料页面的照片。代码如下:
case R.id.textViewTakePhoto:
try
Utility.showPopupMenuWindow(mContext, false, mPopupWindow);
int camera = ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.CAMERA);
if (camera != PackageManager.PERMISSION_GRANTED)
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
else
ActivityCompat.requestPermissions((Activity) mContext,
new String[]android.Manifest.permission.CAMERA,
9);
catch (Exception e)
Log.e(TAG, "Can't access camera" + e.getMessage());
e.printStackTrace();
break;
在我的清单文件中,我添加了以下代码:
<uses-permission android:name="android.permission.CAMERA" />
<!--for camera purposes-->
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
compileSdkVersion
是 25。当我尝试运行应用程序时,它会抛出错误:
Can't access cameraPermission Denial: starting Intent act=android.media.action.IMAGE_CAPTURE cmp=com.google.android.GoogleCamera/com.android.camera.activity.CaptureActivity from ProcessRecord1d685bf 17561:com.dell.engage/u0a215 (pid=17561, uid=10215) with revoked permission android.permission.CAMERA
我该如何解决这个问题
【问题讨论】:
授予运行时权限。 用户可以永久拒绝访问某个应用程序。转到应用程序设置 -> 权限并确保启用了相机权限。此外,您可能希望向用户显示一条消息,说明为什么授予您的应用此权限很重要(如果对它至关重要)。 从您的代码中我可以看到您只是在检查是否授予相机权限,而不是要求用户允许使用相机的权限。 【参考方案1】:您在检查是否授予权限时使用了错误的代码。见以下代码:
if (camera != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions((Activity) mContext,
new String[]android.Manifest.permission.CAMERA,9);
else
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
【讨论】:
此代码不起作用,因为我的代码中的 minSdk 为 16。您的代码位于 minSdk 23 欢迎。很高兴能帮助你。你能分享一下是什么问题吗?【参考方案2】:试试这个代码:
在您的清单中
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
Java 代码
在您的 onClick 方法中:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, RESULT_LOAD_CAMERA);
然后实现onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == RESULT_LOAD_CAMERA && resultCode == RESULT_OK && null != data)
Bundle bundle = data.getExtras();
final Bitmap btm = (Bitmap) bundle.get("data");
mImgView.setImageBitmap(btm);
else
Toast.makeText(MainActivity.this, "no image selected", Toast.LENGTH_LONG).show();
【讨论】:
【参考方案3】:您尚未声明任何权限。试试这样的:
if(ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
else
String[] permissionRequest = Manifest.permission.CAMERA;
requestPermissions(permissionRequest, 8675309);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 8675309)
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
else
Toast.makeText(EnterDataView.this, "Cannot take photo without permission.", Toast.LENGTH_LONG).show();
同时检查您是否在清单文件中授予 CAMERA 权限,如下所示:
<uses-permission android:name="android.permission.CAMERA" />
【讨论】:
【参考方案4】:试试这个代码:
@OnClick(R.id.button_camera)
void onClickCamera()
mPresenter.openCamera(this);
@OnClick(R.id.gallery_button)
void onClickGallery()
mPresenter.openGallery(this);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
mPresenter.startEditing(requestCode, resultCode);
@Override
public void startGallery()
Intent intent = new Intent(HomeActivity.this, GalleryActivity.class);
startActivity(intent);
@Override
public void startCamera(Uri uri)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_CAMERA);
【讨论】:
以上是关于即使在清单文件中声明了适当的权限,也无法访问相机的主要内容,如果未能解决你的问题,请参考以下文章
电脑上为啥会出现此快捷方式引用的项目无法访问,您可能没有适当的权限?