Crop an image when selected from gallery in android
Yes it's possible to crop image in android by using com.android.camera.action.CROP
. after picking image url from gallery.you will start Crop Editor as:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
File file = new File(filePath);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, REQUEST_CROP_ICON);
When the picture select Activity return will be selected to save the contents.in onActivityResult
:
Bundle extras = data.getExtras();
if(extras != null ) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
// The stream to write to a file or directly using the photo
}
and see this post which is also help you for cropping image in android
Crop image on different devices
I found a better code for this problem. This here will search for apps which are able to crop images and start the first that is found. Hope that help someone.
Intent cropApps = new Intent("com.android.camera.action.CROP");
cropApps.setType("image/*");
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(cropApps, 0);
int size = list.size();
if (size == 0)
{
Toast.makeText(context, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return null;
}
else
{
ResolveInfo res = list.get(0);
Intent intent = new Intent();
intent.setClassName(res.activityInfo.packageName, res.activityInfo.name);
intent.setData(imageCaptureUri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_FROM_CAMERA);
}
Related Topics
Filter Listview with Arrayadapter
Cmusphinx Pocketsphinx - Recognize All (Or Large Amount) of Words
Android 5.0 Material Design Style Navigation Drawer for Kitkat
Class Java.Util.Map Has Generic Type Parameters, Please Use Generictypeindicator Instead
Android: Display Image from Sd Card
How to Use Holo.Light Theme, and Fall Back to 'Light' on Pre-Honeycomb Devices
Android Share Intent for a Bitmap - Is It Possible Not to Save It Prior Sharing
Recyclerview Gridlayoutmanager: How to Auto-Detect Span Count
Receive Push Notifications on One Android App from Two Firebase Projects
Cannot Enable Gradle's Offline Mode on Android Studio 3.6
Radiogroup with Two Columns Which Have Ten Radiobuttons
Copy Database from Assets to Databases Folder
Android: Test Push Notification Online (Google Cloud Messaging)
How to Start Power Manager of All Android Manufactures to Enable Background and Push Notification
What Adapter Shall I Use to Use Hashmap in a Listview
Activity Not Started, Its Current Task Has Been Brought to the Front