I have an error when cropping an image from the Gallery.
When I crop the image with a Moto G3 it works.
When I crop the same image with a LG G4 it does not work, the application closes and does not produce any error.
If with the LG G4 I trim the image and make it very small it works correctly.
What's wrong?
public Intent cropImage(Uri picUri) {
Intent cropApps = new Intent("com.android.camera.action.CROP");
cropApps.setType("image/*");
List<ResolveInfo> list = getContext().getPackageManager().queryIntentActivities(cropApps, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(getContext(), "Can not find image crop app", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Recortar imagen", Toast.LENGTH_SHORT).show();
ResolveInfo res = list.get(0);
Intent cropIntent = new Intent();
cropIntent.setClassName(res.activityInfo.packageName, res.activityInfo.name);
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("outputX", 1080);
cropIntent.putExtra("outputY", 1920);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("return-data", true);
return cropIntent;
}
return null;
}
Related
//CROP INTENT
private void doCropping
{
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(selectedImageUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 16);
cropIntent.putExtra("aspectY", 9);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("return-data", true);
try {
((Activity) context).startActivityForResult(cropIntent, code);
} catch (Exception e) {
}
}
//CROP RESULT
private void resultOnCropOkOfGallary(Intent data) {
Bundle extras2 = data.getExtras();
this.bitmap = extras2.getParcelable("data");
if (this.bitmap != null) {
//ivPicture is Imageview.
ivPicture.setImageBitmap(bitmap);
}
}
I get image after cropping perfect.But it makes image quality low.
I got blured image set on imageview. Please need solution.Thank you.
Go for some image cropping libraries.
1) https://github.com/jdamcd/android-crop
2) https://github.com/edmodo/cropper
I'm working with android crop image , this is my code for cropping image :
private void performCrop() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
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", 500);
cropIntent.putExtra("outputY", 500);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "err";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
I have tested this code on android 4.2 , 4.3 and the was no problem , but on android 5,6 , it's returning null pointer exception and I don't know why .
what is wrong with this code ? how can I make it compatible with all version of android ?
You can use according to Build SDK
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
performCrop(fileUri);
} else {
performCropImage(fileUri);
}
// code for device below 5
private boolean performCropImage(Uri mFinalImageUri) {
Uri mCropImagedUri;
try {
if (mFinalImageUri != null) {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mFinalImageUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
// cropIntent.p
//indicate output X and Y
cropIntent.putExtra("outputX", 200);
cropIntent.putExtra("outputY", 200);
//retrieve data on return
cropIntent.putExtra("return-data", false);
File f = createNewFile("CROP_");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}
mCropImagedUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImagedUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
return true;
}
} catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = getString(R.string.crop_not_supported);
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
return false;
}
return false;
}
// code for 5 or 6
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
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", 200);
cropIntent.putExtra("outputY", 200);
// 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 = getString(R.string.crop_not_supported);
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
On ActivityResult put this code
Uri imageUri = data.getData();
try {
Bitmap selectedBitmap;
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
Bundle extras = data.getExtras();
selectedBitmap = extras.getParcelable("data");
} else {
selectedBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
// I hope you this code help you
I'm trying to get crop image working on Samsung s4 but it's not showing the crop screen.
This is the code that I'm using.
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);
intent.putExtra("outputX", 180);
intent.putExtra("outputY", 220);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
} catch (ActivityNotFoundException e) {
// Do nothing for now
String s = e.getMessage();
String test = e.getLocalizedMessage();
}
}
Intent which you are using is for capturing image.
You need to first capture image and then apply crop on it.
Yo can do like this.
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
}
catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
onActivityResult where you get captured image. apply crop on that.
private void performCrop() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP"); cropIntent.setDataAndType(mUri, "image/*");
// set crop properties
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", 150);
cropIntent.putExtra("outputY", 150);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 12);
}
// 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.makeText(Signature.this, errorMessage, Toast.LENGTH_SHORT)
.show();
}
}
i want crop photo that i take from camera, so far i try do it like this but with no success
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
getActivity().startActivityForResult(intent, REQUEST_CAMERA);
is it possible to do it without any 3th part libraries.?
i checked https://github.com/biokys/cropimage
but it doesnt gave me any results
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
String path = data.getStringExtra(CropImage.IMAGE_PATH);
// if nothing received
if (path == null) {
return;
}
// cropped bitmap
Bitmap bitmap = BitmapFactory.decodeFile(path);
((ImageView) findViewById(R.id.userpicture)).setImageBitmap(bitmap);
}
is it possible to do it without any 3th part libraries.?
Not reliably. Android does not have a CROP Intent.
i checked https://github.com/biokys/cropimage but it doesnt gave me any results
Perhaps you did not integrate it properly. In addition to the libraries mentioned in my blog post (linked to above), the Android Arsenal has a few options.
try this
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
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(getActivity(), 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);
}
}
}
I have the following code for users to crop image. When I set the size beyond 256, it does not work. My gut feel is "cropIntent.putExtra("return-data", true);" causing the error. How do I pass in the uri to cropIIntent and retrieve out from onActivityResults? In another words, save the image after crop and retrieve.
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} //respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
try {
final TextView imgTv = (TextView) findViewById(R.id.imageInfo);
Bundle extras = data.getExtras();
thumbnail = extras.getParcelable("data");
ImageView image = (ImageView) findViewById(R.id.pestImage);
image.setImageBitmap(thumbnail);
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) {
f.delete();
}
}
}//end onactivity results
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
//indicate output X and Y
cropIntent.putExtra("outputX", 800);
cropIntent.putExtra("outputY", 800);
File f = new File(Environment.getExternalStorageDirectory(),
"/temporary_holder.jpg");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}
uri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(cropIntent, PIC_CROP);
} //respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
String filePath = Environment.getExternalStorageDirectory()
+ "/temporary_holder.jpg";
thumbnail = BitmapFactory.decodeFile(filePath);
//thumbnail = BitmapFactory.decodeFile(filePath);
// Log.i("",String.valueOf(thumbnail.getHeight()));
ImageView image = (ImageView) findViewById(R.id.pestImage);
image.setImageBitmap(thumbnail);
}}
Try
Uri cropedImageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(cropedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Now filePath is the path to cropped image
To load bitmap from filePath Use
private Bitmap loadImageFromSDCard(String filePath) {
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 1;
bfo.outWidth = 100;
bfo.outHeight = 100;
Bitmap photo = BitmapFactory.decodeFile(filePath, bfo);
return photo;
}
if (android.os.Build.VERSION.SDK_INT > 10){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
int gcd = BigInteger.valueOf(ImageWidth).
gcd(BigInteger.valueOf(ImageHeight)).intValue();
cropIntent.putExtra("aspectX", (ImageWidth / gcd));
cropIntent.putExtra("aspectY", (ImageHeight / gcd));
cropIntent.putExtra("outputX", ImageWidth); // X
cropIntent.putExtra("outputY", ImageHeight); // Y
cropIntent.putExtra("return-data", true);
cropIntent.setData(picUri);
startActivityForResult(cropIntent, PIC_CROP_INTENT_ID);
}