Crop image in android
Can you use default android Crop functionality?
Here is my code
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties here
cropIntent.putExtra("crop", true);
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
declare:
final int PIC_CROP = 1;
at top.
In onActivity result method, writ following code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
imgView.setImageBitmap(selectedBitmap);
}
}
}
It is pretty easy for me to implement and also shows darken areas.
How to auto center crop ImageView Android?
I am not sure if I understood correctly. It must be either of the two, that you want, I guess.
In your image view set the attribute
android:scaleType="fitXY"
to fit the ImageView
completely.
You can choose
android:scaleType="centerCrop"
to crop Center.
How to crop an image in Android
You can crop an image using an onActivityResult()
.
This video shows how to set it up properly with setOnClickListener()
.
Don't forget to set the implementation
in your build.gradle
:
implementation 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
Good luck! :)
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
How to crop images in android?
You can try using this Cropper Library
Its a great image cropping library. I have used it mostly in my apps.
Here is a screenshot of the library in action:
I hope you can find it useful. Cheers! :)
Related Topics
How to Solve the Install_Failed_Dexopt Error
Creating Custom Lockscreen in Android
Calling Setcontentview() Multiple Times
Best Practice for Nested Fragments in Android 4.0, 4.1 (<4.2) Without Using the Support Library
Mediaplayer Stutters at Start of Mp3 Playback
Programmatically Relaunch/Recreate an Activity
How to Open/Display Documents(.Pdf, .Doc) Without External App
Getting Access to Media Player Cache
Can't Run a Java Android Program with Valgrind
How to Make Circle Custom Progress Bar in Android
Emulator in Android Studio Doesn't Start
Geocoder.Getfromlocation Throws Ioexception on Android Emulator
Android SQLite Issue - Table ... Has No Column Named
Add a Background Image to Shape in Xml Android
How to Check If a View Is Visible or Not in Android
How to Use Onsaveinstancestate() and Onrestoreinstancestate()