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();
}
}
Related
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;
}
//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 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 am using following code to pick image from gallery
public void takePhotoFromLibrary() {
_isFromLogin = false;
try {
// Launch picker to choose photo for selected contact
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
// intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG
.toString());
intent.putExtra("noFaceDetection", false);
startActivityForResult(intent, PHOTO_PICKED);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
I am using Lg optimus p350 for testing. In this case when i select an image picked by camera onActivityForResult is not getting called. Can anyone please help me with this?
It may be help you..
http://android-er.blogspot.com/2011/02/select-image-using-android-build-in.html
How to pick an image from gallery (SD Card) for my app?
If you use this intent instead to launch the pick image activity:
Intent i = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST_CODE);
Then you will get a callback to:
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent intent) { }
when the user has selected an image.