I want to crop an image in my application when it is selected from gallery.
i.e if I launch the gallery and select an image the cropping window should come like when we select an image from iPhone. Is it possible in Android.
I found one tutorial for cropping the image in android, but dont seem the way I wanted.
http://www.coderzheaven.com/2011/03/15/crop-an-image-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
This tutorial is exactly what you need enjoy:
Picking image from gallery:
Crop image after Intent pick action
Cheers
You can already tell the Camera/Gallery-Intent to start the crop-editor after the image is selected/taken:
Pick Image from Gallery:
Intent pickImageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickImageIntent.setType("image/*");
pickImageIntent.putExtra("crop", "true");
pickImageIntent.putExtra("outputX", 200);
pickImageIntent.putExtra("outputY", 200);
pickImageIntent.putExtra("aspectX", 1);
pickImageIntent.putExtra("aspectY", 1);
pickImageIntent.putExtra("scale", true);
pickImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriWhereToStore);
pickImageIntent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(pickImageIntent, RESULT_LOAD_IMAGE);
Take Photo:
Intent takePicIntent = new Intent("android.media.action.IMAGE_CAPTURE");
takePicIntent .putExtra("crop", "true");
takePicIntent .putExtra("outputX", 200);
takePicIntent .putExtra("outputY", 200);
takePicIntent .putExtra("aspectX", 1);
takePicIntent .putExtra("aspectY", 1);
takePicIntent .putExtra("scale", true);
takePicIntent .putExtra(MediaStore.EXTRA_OUTPUT, uriWhereToStore);
takePicIntent .putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(takePicIntent , RESULT_LOAD_IMAGE);
Although part of the internal API, the com.android.camera.action.CROP seems like it is well-supported on most Android devices. This might get you started:
final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(uriOfImageToCrop);
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", Uri.fromFile(someOutputFile));
startActivityForResult(intent, SOME_RANDOM_REQUEST_CODE);
Then handle what you need to do in the onActivityResult() method of your Activity. Your output file should be the cropped image.
Since this Intent action is part of the internal API, however, I would strongly advise that you have some sort of fallback behavior if some device does not support the Intent. Some manufacturers provide their own Gallery apps and so there is no way of knowing whether or not the user's device will recognize the Intent. PLEASE DON'T FORGET THIS!! :)
I solved this problem this way
private void pickUserImage() {
if (doHavePermission()) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("scale", true);
photoPickerIntent.putExtra("outputX", 256);
photoPickerIntent.putExtra("outputY", 256);
photoPickerIntent.putExtra("aspectX", 1);
photoPickerIntent.putExtra("aspectY", 1);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
startActivityForResult(photoPickerIntent, PICK_FROM_GALLERY);
}
}
find my complete solution here in stackoverflow post
Nobody will tell you which extra is needed unless you try it:
val intent = Intent(Intent.ACTION_PICK)
intent.apply {
type = "image/*"
putExtra("crop", "true") // NOTE: should be string
putExtra("outputX", 300) // This is needed, editor can't close without these two
putExtra("outputY", 300) // This is needed
putExtra("scale", true)
putExtra("aspectX", 1)
putExtra("aspectY", 1)
putExtra("return-data", true)
}
startActivityForResult(intent, YOUR_INT_CODE)
Related
Hello fellow programmers. My problem: Every time I made a photo, Android automatically scales it down to 160x160px. I don't know why. Here is my code:
Here I set all the preferences for the camera:
public void ChosenPhoto(String extra) {
String gallery = "gallery";
String camera = "camera";
if (extra.equals(gallery)) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("return-data", true);
intent.putExtra("aspectX", 560);
intent.putExtra("aspectY", 560);
intent.putExtra("outputX", 560);
intent.putExtra("outputY", 560);
intent.putExtra("noFaceDetection", true);
intent.putExtra("screenOrientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(intent, PICK_FROM_GALLERY);
} else if (extra.equals(camera)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 560);
intent.putExtra("aspectY", 560);
intent.putExtra("outputX", 560);
intent.putExtra("outputY", 560);
intent.putExtra("noFaceDetection", true);
intent.putExtra("screenOrientation", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
}
Here I use it later on:
Bundle extras = data.getExtras();
photoBit = extras.getParcelable("data");
ByteArrayOutputStream PhotoStream = new ByteArrayOutputStream();
photoBit.compress(Bitmap.CompressFormat.PNG, 100, PhotoStream);
photos = PhotoStream.toByteArray();
Log.e(TAG, "Width:" + photoBit.getWidth() );
ImageView.setImageBitmap(photoBit);
In the ImageView, I later see that it is scaled down. More precise: every time to 160x160px... I have no idea why.
Please help me. If you need more information, just ask for it.
What is returned in "data" is actually only a thumbnail and not the full-sized photo. It is not possible to get the full-sized image this way, but you need to create a file in which the camera will save it for you. Working example with explanation can be found from http://developer.android.com/training/camera/photobasics.html under title "Save the Full-size Photo".
First you need to create a new File that will store the image. You may want to make sure the file name does not collide with other files (append timestamp).
File file = new File(context.getExternalFilesDir( null ), "photo.jpg");
Then you specify this in the intent:
intent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( file ) );
activity.startActivityForResult( intent, REQUEST_TAKE_PHOTO );
In onActivityResult() you will check if photo was actually taken. You will use the File instance that you have created before that now has the image.
if ( ( requestCode == REQUEST_TAKE_PHOTO ) && ( resultCode == RESULT_OK ) ) {
If you want to put the image from the file into ImageView, you can do this:
Bitmap bmp = BitmapFactory.decodeFile( file.getAbsolutePath() );
imageView.setImageBitmap(bmp);
In my application the user selects an image. When the CROP_IMAGE intent is launched it displays the dialog to select one of the available image croppers installed on the device. I would like to solve that one of the installed programs launch default that user don't have to choose all time which program wants to use.
Is it possible to skip this chooser dialog and launch one image cropper automatically?
I found an answer :
Intent cropApps = new Intent("com.android.camera.action.CROP");
cropApps.setType("image/*");
List<ResolveInfo> list = this.getPackageManager().queryIntentActivities(cropApps, 0);
int size = list.size();
if (size == 0)
{
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
}
else
{
ResolveInfo res = list.get(0);
Intent cropIntent = new Intent();
cropIntent.setClassName(res.activityInfo.packageName, res.activityInfo.name);
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("outputX", 800);
cropIntent.putExtra("outputY", 800);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
I use the following code to let the user pick a picture from the gallery. Then the user can crop it. The result is written in an output file. The problem is that the output file has much less quality than the original. Is there a way to have it without quality loose.
Thanks.
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
File fileOut = new File(Environment.getExternalStorageDirectory() + "/Outputfile.png");
intent.putExtra("output", Uri.fromFile(fileOut));
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
startActivityForResult(Intent.createChooser(intent, getString(R.string.loEditarPerfiles_SeleccioneImagen)), 1234567);
I have an issue: need to take photo with camera and then show screen with possibility to crop photo that is taken. I added suchfunction:
protected void callCropPhoto(Uri uri) {
try {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", avatar.getWidth());
intent.putExtra("aspectY", avatar.getHeight());
intent.putExtra("outputX", avatar.getWidth());
intent.putExtra("outputY", avatar.getHeight());
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_PHOTO_REQUEST_CODE);
} catch (Exception ex) {
// Some devices can't support image cropping.
}
}
When I get result from camera with photo I call this function with temporary uri to taken photo. When calls onActivityResult usualy I get RESULT_OK and data - all works good. But on one device (android version 2.3.4) in landscape mode I got RESULT_CANCELED and no screen with cropping appear. What is the problem?
so I want to choose an image from gallery and then crop it:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), PHOTO_PICKED_WITH_DATA);
OK, pick the photo and then catch it onActivityResult, then crop it:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mAvatarUri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", ICON_SIZE);
intent.putExtra("outputY", ICON_SIZE);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mAvatarUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_IMAGE);
now, the problem is when i want to transform it into bytes and then send it inside an xml... it doesn't take only the cropped image but instead the whole image itself...
also, i cannot access cropped image uri, it says file not found!
hmmmm, seems like my cropped image weren't saved after all...
how can i suppose to fix this?
Samsung ACE 2.3.4
Check this code in the following link.
Crop Image
It works fine for me..
I don't know how you got that technique to crop image. But, for me, I always use this library. And it never failed to impress me. Work from android 2.1 all the way to 3.2 (never test it on 4.0 onward).
Here's how I do it:
Intent cropIntent = new Intent(imageProcessActivity,
CropImage.class);
cropIntent.putExtra("image-path",
FileUtil.saveTempFile(ImageProcessActivity.processedBitmap, filename));
cropIntent.putExtra("scale", true);
imageProcessActivity.startActivityForResult(cropIntent, ImageProcessActivity.INTENT_CROP_CODE);
and here's how to catch the result:
if (requestCode == INTENT_CROP_CODE && resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
if (extras != null) {
Uri uri = null;
uri = (Uri) extras.get("imageCrop");
Bitmap bitmap = null;
try {
bitmap = ImageUtil.decodeFile(
new File(new URI(uri.toString())),
AppConstant.MAX_IMAGE_SIZE);
} catch (URISyntaxException e) {
e.printStackTrace();
}
processedBitmap = bitmap;
selectedImage.setImageBitmap(bitmap);
}
}