问题描述
我正在使用"com.android.camera.action.CROP"从用户的图片库中裁剪图像.
I am using "com.android.camera.action.CROP" to crop images from user`s image gallery..
裁剪功能:
private void performCrop(String picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
cropIntent.putExtra("crop", "false");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 1024); //512
cropIntent.putExtra("outputY", 1024); //512
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, RESULT_CROP);
}
catch (ActivityNotFoundException e) {
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
并将结果设置为图像视图:
and set the result to image view:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
if(resultCode == Activity.RESULT_OK){
picturePath = data.getStringExtra("picturePath");
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP ) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
addImageImageView.setImageBitmap(selectedBitmap);
addImageImageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
}
在某些设备上,裁切操作会返回模糊的小尺寸图像(例如4128 * 2322像素的图像变为160 * 160像素的裁切图像).在大多数设备上,它运行良好.我不确定outputX和Y到底是做什么的,但是更改它们并不能解决我的问题.
On some devices, crop action returns a blurry and small size image (eg. a 4128*2322 pixel image turns to a 160*160 pixel cropped image). While on most devices, it works great. I am not sure what outputX and Y are really do, but changing them doesn`t solve my problem.
推荐答案
否,Android 没有具有作物意图
许多开发人员正在对Intent调用 startActivity(),其操作为 com.android.camera.action.CROP .他们正在这样做以裁剪图像.
No, Android Does Not Have a Crop Intent
Many developers are calling startActivity() on an Intent with an action of com.android.camera.action.CROP. They are doing this to crop an image.
这是一个非常糟糕的主意.
This is a really bad idea.
在这种特定情况下,AOSP Camera应用程序支持此Intent操作.该应用程序并非在所有设备上都存在.缺少此应用程序的设备将不会对此未记录的Intent操作做出响应,并且您的应用程序将崩溃.
In this specific case, this Intent action is supported by the AOSP Camera app. That app does not exist on all devices. Devices lacking this app will not respond to this undocumented Intent action, and your app will crash.
有几个用于在Android中裁剪图像的开源库.我没有尝试过其中任何一个,因此无法保证任何工作做得如何.但是,与依靠任何特定用户设备上可能不存在的应用程序中未记录的Intent操作相比,它们是更安全的选择.
There are several open source libraries for cropping images in Android. I have not tried any of them, and therefore cannot vouch for how well any work. However, they are safer bets than relying upon an undocumented Intent action from an app that may not exist on any given user’s device.
以下是一些需要考虑的库:
Here are some libraries to consider:
- android-crop Image
- crop-Image
- crop-Image (forked from the above one)
- pick-n-crop
希望这可以解决您的问题.
Hope this solved your problem.
这篇关于使用"com.android.camera.action.CROP";意图裁剪图像,在某些设备上返回小尺寸位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!