我只是想用相机拍照,我正在处理权限,调试器正在返回java.io.IOException: Permission denied
,但我无法识别错误。
这里是相机按钮:
btn_cam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
camera = new CameraPicture(getApplicationContext(), 10, 15);
String[] permissionRequested = {Manifest.permission.CAMERA};
requestPermissions(permissionRequested, camera.getPermissionRequest());
}
});
}
这是处理相机的CameraPicture
类的有趣代码:
public CameraPicture(Context context, int act_res_req_num, int perm_req_num) {
this.context = context;
this.cam_activity_result_req_num = act_res_req_num;
this.cam_perm_req_num = perm_req_num;
}
public Intent takePhotoIntent() throws IOException {
File photoFile;
Intent in = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (in.resolveActivity(this.context.getPackageManager()) != null && (photoFile = this.createImageFile()) != null) {
in.putExtra("output", (Parcelable)Uri.fromFile((File)photoFile));
}
return in;
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory((String)Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
this.photoPath = image.getAbsolutePath();
return image;
}
这里似乎捕获异常的权限方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if ((requestCode==camera.getPermissionRequest()) && (grantResults[0]==PackageManager.PERMISSION_GRANTED)){
try {
startActivityForResult(camera.takePhotoIntent(), camera.getActResultRequest());
} catch (IOException e) {
e.printStackTrace();
System.out.println("ERROR MESSAGE: "+e);
Toast.makeText(getApplicationContext(),"Error taking the picture ",Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(getApplicationContext(),"Unable to accesss the camera without permission",Toast.LENGTH_LONG).show();
}
}
你看到有什么不对吗?
编辑:忘了展示清单
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
答案 0 :(得分:1)
试试这个,
private static final int REQUEST_CAMERA = 112;
btn_cam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.CAMERA};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST_CAMERAE );
} else {
camera = new CameraPicture(getApplicationContext(), 10, 15);
//open camera
}
} else {
camera = new CameraPicture(getApplicationContext(), 10, 15);
//open camera
}
}
});
}
/*get Permissions Result*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CAMERA: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
camera = new CameraPicture(getApplicationContext(), 10, 15);
//open camera
}
}
}
}
/*check permissions for marshmallow*/
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}