我想拍摄照片或选择照片后裁剪从URI的图像。而我的codeS是这样的:
I tried to crop an image from a Uri after taking a photo or picking a picture. And my codes are like these:
public static void cropImage(Uri uri, Activity activity, int action_code) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 600);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intent, action_code);
} else {
Toast.makeText(activity, "No Crop App Available", Toast.LENGTH_SHORT).show();
}
}
和overridding 的onActivityResult()
是这样的:
And overridding onActivityResult()
like this:
if (resultCode == Activity.RESULT_OK && requestCode == Utils.CODE_CROP_IMAGE) {
Bundle extras = data.getExtras();
showCenterToast("ccc");
if (extras != null) {
showCenterToast("CCC");
Bitmap photo = extras.getParcelable("data");
ivAvatar.setImageBitmap(photo); // display image in ImageView
FileOutputStream fos = null;
try {
fos = new FileOutputStream(Utils.AVATAR_FILE);
photo.compress(Bitmap.CompressFormat.PNG, 100, fos);// (0-100)compressing file
showCenterToast("DDD");
Utils.AVATAR_FILE_TMP.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
IoUtil.closeSilently(fos);
}
}
}
在Android中pre-棒棒糖设备,我能获得位图图片
并在的ImageView 。 但是,在Android上棒棒堂,我总是
空
从 data.getExtras();
On devices in Android Pre-Lollipop, I was able to obtain Bitmap photo
and display it in an ImageView
. But, on Android Lollipop, I always got null
from data.getExtras();
.
我GOOGLE了很多,但有大约在Android棒棒糖裁剪图片
I googled a lot but got few useful things about cropping an image on Android Lollipop.
Android的修改 com.android.camera.action.CROP
的裁剪棒棒糖上其返回机制。那么,什么是新机制?我怎么能得到返回位图
裁剪上棒棒堂之后?
Android changed its returning mechanism of cropping of com.android.camera.action.CROP
on Lollipop. So, what is the new mechanism? How could I get the returned Bitmap
after cropping on Lollipop?
任何提示将AP preciated。先谢谢了。
Any tips will be appreciated. Thanks in advance.
推荐答案
我觉得你的问题无关,与Android版本,但你想要的图像进行裁剪。裁剪类处理后的图像 com.android.gallery3d.filtershow.crop.CropActivity#BitmapIOTask
。当图像过大回报,它会试图返回图像的拇指,并且将返回null有时。为了避免这种情况,可以通过设置让裁剪图像,而不是位图的URI intent.putExtra(MediaStore.EXTRA_OUTPUT,tmpUri);
,其中 tmpUri
是用于保存结果的URI。然后你可以从位图 tmpUri
。
I think that your problem has nothing to do with Android version but the image you want to be cropped. Cropping image processed in class com.android.gallery3d.filtershow.crop.CropActivity#BitmapIOTask
. When the image is too large to return, it will try to return the thumb of the image, and will return null sometimes.
To avoid this, you can get the uri of cropped image instead of bitmap by setting intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
where tmpUri
is an uri created to hold the result. And then you can get the bitmap from tmpUri
.
样code:
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
private static Uri tmpUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap
public static void cropImage(Uri uri, Activity activity, int action_code) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 600);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
if (intent.resolveActivity(activity.getPackageManager()) != null) {
activity.startActivityForResult(intent, action_code);
} else {
Toast.makeText(activity, "No Crop App Available", Toast.LENGTH_SHORT).show();
}
}
和在功能上的onActivityResult:
And in function onActivityResult:
if (resultCode == Activity.RESULT_OK && requestCode == Utils.CODE_CROP_IMAGE) {
// Bundle extras = data.getExtras();
Uri uri = data.getData();
showCenterToast("ccc");
if (uri != null) {
showCenterToast("CCC");
// Bitmap photo = null;
// if (tmpUri != null) {
// photo = decodeBitmapFromUri(tmpUri); // Get bitmap from uri.
// }
Bitmap photo = decodeBitmapFromUri(uri);
ivAvatar.setImageBitmap(photo); // display image in ImageView
FileOutputStream fos = null;
try {
fos = new FileOutputStream(Utils.AVATAR_FILE);
photo.compress(Bitmap.CompressFormat.PNG, 100, fos);// (0-100)compressing file
showCenterToast("DDD");
Utils.AVATAR_FILE_TMP.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
IoUtil.closeSilently(fos);
}
} else {
showCenterToast("Uri is NULL");
}
}
private Bitmap decodeUriAsBitmap(Uri uri){
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
我没有测试我的code是否是正确的,但我认为你可以修复的错误。
I have not test whether my code was correct, but I think you can fix the bugs.
相关推荐
最新文章