我正在编写一个简单的库存管理应用程序,用于记录要记录的项目。因此,预期的行为是用户点击按钮启动相机,拍照并返回应用程序以输入文本信息。但我不断收到以下安全例外错误
java.lang.SecurityException: Permission Denial: starting Intent {
act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera }
from ProcessRecord{734dbfd 22169:com.virgo19.tinni.teatracker/u0a58}
(pid=22169, uid=10058) with revoked permission android.permission.CAMERA
我已经浏览了几天网络,似乎没有任何解决方案,因为我已经按照Android开发人员的说明进行操作。包括有关在运行时请求权限的说明。下面的代码片段,
//Code calling camera intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, RETURN_FROM_CAMERA);
}
//On activity return request fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RETURN_FROM_CAMERA && data != null){
//Check permissions
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
//Permission is okay, so get on with getting image from Camera
Bundle extras = data.getExtras();
tinImage = (Bitmap) extras.get("data");
//set image view
setImage();
} else if (permissionCheck != PackageManager.PERMISSION_GRANTED){
//Permission not granted, ask for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA);
}
}
}
有人能看出为什么会一直崩溃吗?谢谢!
答案 0 :(得分:2)
您需要在通过CAMERA
拍摄照片之前申请startActivityForResult()
权限。 拍摄照片后,您当前的代码会尝试请求此权限。