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();
}
}
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);
}
}
}
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();
.
I googled a lot but got few useful things about cropping an image on Android Lollipop.
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?
Any tips will be appreciated. Thanks in advance.
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 settingintent.putExtra(MediaStore.EXTRA_OUTPUT, tmpUri);
wheretmpUri
is an uri created to hold the result. And then you can get the bitmap fromtmpUri
.Sample code:
And in function onActivityResult:
I have not test whether my code was correct, but I think you can fix the bugs.
Use intent to get image:
Use Activity Result to receiver data and get Uri.
Use android-crop to crop and get image.
On Lollipop, the cropped image is returned as a String reference to a uri in the result data action. Example:
I Solved It and Working Now
Basically when cropping image in fragment the issue is here, when you use activity you should pass intent in this way in on activity result for best approach to cropping follow this library
compile 'com.theartofdev.edmodo:android-image-cropper:2.5.+'.
When you use Fragment you should use:
Sometimes getcontext requires api 23 this is because you using app.fragment, so use android.support.v4.app.
Cropping Image with activity and Fragment is Different, see here i have Successfully Implement this thing.follow the link here, for guideline. how cropping image with in activity and fragment !!!
I was feeling difficulty while cropping image in fragment so its not solved. First you take image from camera or gallery.
In case of taking image from camera don't forget to include file provider Manifest fil , if you fee trouble then before doing next follow this link.
Now on activity result for Fragment Activity you have to write this code.