从android中的图库中选择时裁剪图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10776438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:55:40  来源:igfitidea点击:

Crop an image when selected from gallery in android

android

提问by user1293519

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.

当我从图库中选择图像时,我想在我的应用程序中裁剪图像。即,如果我启动图库并选择一个图像,当我们从 iPhone 中选择一个图像时,裁剪窗口应该会出现。在安卓中可以吗?

I found one tutorial for cropping the image in android, but dont seem the way I wanted.

我找到了一个在 android 中裁剪图像的教程,但似乎不是我想要的方式。

http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/

http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/

回答by ρяσ?ρ?я K

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:

是的,可以使用com.android.camera.action.CROP. 从图库中选择图像 url 后,您将启动裁剪编辑器:

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:

当图片选择Activity返回时会选择保存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 postwhich is also help you for cropping image in android

并查看这篇文章,它也可以帮助您在 android 中裁剪图像

回答by K_Anas

This tutorialis exactly what you need enjoy:

教程正是您需要享受的:

Picking image from gallery:

从图库中选择图像:

enter image description here

在此处输入图片说明

Crop image after Intent pick action

在 Intent 选择操作后裁剪图像

enter image description here

在此处输入图片说明

Cheers

干杯

回答by Till

You can already tell the Camera/Gallery-Intent to start the crop-editor after the image is selected/taken:

在选择/拍摄图像后,您已经可以告诉 Camera/Gallery-Intent 启动裁剪编辑器:

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);

回答by Alex Lockwood

Although part of the internal API, the com.android.camera.action.CROPseems like it is well-supported on most Android devices. This might get you started:

虽然是内部 API 的一部分,但com.android.camera.action.CROP它似乎在大多数 Android 设备上都得到了很好的支持。这可能会让你开始:

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.

然后在onActivityResult()你的Activity. 您的输出文件应该是裁剪后的图像。

Since this Intentaction is part of the internal API, however, I would strongly advisethat 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!!:)

Intent但是,由于此操作是内部 API 的一部分,因此我强烈建议您在某些设备不支持Intent. 一些制造商提供他们自己的图库应用程序,因此无法知道用户的设备是否会识别Intent. 请不要忘记这个!!:)

回答by Gaurav

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 herein stackoverflow post

找到我的完整的解决方案在这里在后计算器

回答by BollMose

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)