我在我的应用中发现了一个错误。相机意图不仅仅在三星s7中打开。 在运行时授予权限,并且我在手机中对其真正授予的权限进行了双重检查。 我仍然无法打开相机拍照或打开照片。 任何想法?
private void takePicture() throws IOException {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = AppUtils.createImageFile(AppUtils.ImageType.JPG, AppGlobals.APP_NAME);
} catch (Exception e) {
AppLog.error(TAG, String.format("Failed to create file for saving the picture - %s",
e.getMessage()));
}
// Continue only if the File was successfully created
if (photoFile == null) {
throw new IOException(getString(R.string.failed_to_take_picture));
}
currentImagePath = photoFile.getAbsolutePath();
AppUtils.takePhotoFromFragment(this, RequestCodes.TAKE_PICTURE, photoFile);
}
public static File createImageFile(ImageType type, String internalFolder) throws Exception {
// Create an image file name
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
Calendar cal = Calendar.getInstance(Locale.US);
String timeStamp = dateFormat.format(cal.getTime());
File storageDir;
if(TextUtils.isEmpty(internalFolder)){
storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
}else{
storageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM),
internalFolder);
}
if(!storageDir.exists()){
if(!storageDir.mkdir()){
return null;
}
}
File image;
String imageFileName;
String imageType;
if(type == ImageType.PNG){
imageType = "png";
imageFileName = timeStamp;
}else{
imageType = "jpg";
imageFileName = timeStamp;
}
image = new File(storageDir, String.format("%s.%s", imageFileName, imageType));
if(image.createNewFile()){
return image;
}
return null;
}
public static void takePhotoFromFragment(Fragment fragment, int requestCode, File imageFile){
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(fragment.getActivity().getPackageManager()) != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(imageFile));
fragment.startActivityForResult(takePictureIntent, requestCode);
}
}catch (Exception ex){
Log.e(TAG, "Failed to open camera for taking picture!");
}
}