我试图用相机拍照,之后将其裁剪成ImageView。
不幸的是,您将在下面的代码中看到的crop方法中的startActivityForResult将始终返回错误结果代码。
我认为我找到了对此负责的坏人,但我不知道如何解决这个问题。
首先是代码:
捕捉图片:
/**
* Starts Intent to open camera and take picture
*/
private void captureImage() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(this, "Whoops - could not access external storage!", Toast.LENGTH_SHORT).show();
}
if (photoFile != null) {
Uri photoUri = FileProvider.getUriForFile(this, FILEPROVIDER_AUTHORITY, photoFile);
contact.setImage(Uri.fromFile(photoFile));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
try {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Whoops - something went wrong!", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(this, "Whoops - your device doesn't support capturing images!", Toast.LENGTH_SHORT).show();
}
}
裁剪图片:
/**
* Starts (unofficial) Intent to crop chosen image which is likely to fail
*/
private void cropImage() {
Uri photoUri = FileProvider.getUriForFile(this, FILEPROVIDER_AUTHORITY,new File(contact.getImage().getPath()));
grantUriPermission("com.android.camera", photoUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(photoUri, "image/*");
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
cropIntent.putExtra("outputFormat", "JPEG");
cropIntent.putExtra("noFaceDetection", true);
cropIntent.putExtra("return-data", true);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
try {
startActivityForResult(cropIntent, REQUEST_IMAGE_CROP);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Whoops - your device doesn't support the crop action!", Toast.LENGTH_SHORT).show();
}
}
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_IMAGE_CAPTURE:
cropImage();
break;
case REQUEST_IMAGE_CHOOSE:
break;
case REQUEST_IMAGE_CROP:
Bitmap tmp = data.getExtras().getParcelable("data");
previewImageView.setImageBitmap(tmp);
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
} else {
Toast.makeText(this, "Whoops - something went wrong!", Toast.LENGTH_SHORT).show();
}
}
现在我认为负责的坏人是com.android.gallery3d应用程序,因为每次触发裁剪意图时都会抛出此异常:
com.android.gallery3d W/CropActivity: cannot open region decoder for file: content://de.hska.mycontacts.fileprovider/contact_images/CONTACT_20180413_155111_.jpg
java.io.IOException: Image format not supported
at android.graphics.BitmapRegionDecoder.nativeNewInstance(Native Method)
at android.graphics.BitmapRegionDecoder.newInstance(BitmapRegionDecoder.java:124)
at com.android.gallery3d.filtershow.crop.CropActivity$BitmapIOTask.doInBackground(CropActivity.java:483)
at com.android.gallery3d.filtershow.crop.CropActivity$BitmapIOTask.doInBackground(CropActivity.java:421)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
com.android.gallery3d W/CropActivity: cannot decode file: content://de.hska.mycontacts.fileprovider/contact_images/CONTACT_20180413_155111_.jpg
我希望有人可以帮助我:)
答案 0 :(得分:0)
正如CommonsWare已在评论中说明,意图com.android.camera.action.CROP
不是正式的,并且不适用于大多数设备。因此,使用库是一种更好的做法。
我最终使用的uCrop非常易于使用,并且还支持大量自定义。