How to crop an image whose URI is known in Android?

I want the image to be displayed in the image that needs to be selected with a specific part (and only the selected part should be selected and the other part translucent), and this part can also be changed as needed or made by the user by touching.

Now the selected part of the image is necessary for cropping, and then it will show and save this cropped image.

EDIT:

I used Intent to open the image and crop it with aim.putExtra ("crop", "true");

But when passing the intention, I want to open an image whose URI is already known, rather than opening the entire album of the image gallery.

Can anyone suggest how I can open a specific URI with the intention of transmitting the opening image. Thanks in advance.

+4
source share
4 answers

UPDATE: changed my answer after the question has been edited and more accurately described.

The problem you are facing has a long history, also in SO:

could not find com.android.camera.CropImage activity in android

One answer describes what you need. Please note that you are using an intent that is not part of the official SDK, and you may encounter various problems. The problem I encountered immediately used the crop after the image was captured by the camera. In addition, it is not compatible with various versions of Android, so if you work at 1.5, it may not work at 2.3. Other useful links:

http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/569f36b5b28f2661?lnk=gst&q=Crop+image+intent#569f36b5b28f2661

http://groups.google.com/group/android-developers/browse_thread/thread/2dd647523926192c/dcbe5aef29eddad6?lnk=gst&q=Crop+image+intent#dcbe5aef29eddad6

http://groups.google.com/group/android-developers/browse_thread/thread/d7b6a133c164aa17/184bf3b85da2ce58?lnk=gst&q=Crop+image+intent#184bf3b85da2ce58

+2
source

As for the last part of your question, if you are on the very latest Gingerbread (2.3.3, API level 10), you can use BitmapRegionDecoder to crop the image.

This is useful because, until this API existed, you had to load the entire image into memory before you could crop. With 5mpix and 8mpix cameras, this is usually not possible without subsampling (i.e., the cropped image loses a lot of resolution).

+3
source

See my answer to this question . It does not concern the aspect of touching the size of your question, but processes parts of the image above the original image.

On the bottom line, you do not want to use ImageView, since it is mainly for displaying a static image with various scaling properties. You are better off using a custom view with an overridden draw () method.

+1
source

To crop an image

 private void cropImage() { // Use existing crop activity. Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(capturedImageUri, IMAGE_UNSPECIFIED); // Specify image size intent.putExtra("outputX", IMAGE_DIMENSION); intent.putExtra("outputY", IMAGE_DIMENSION); // Specify aspect ratio, 1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("scale", true); intent.putExtra("return-data", true); // REQUEST_CODE_CROP_PHOTO is an integer tag you defined to // identify the activity in onActivityResult() when it returns startActivityForResult(intent, REQ_CODE_CROP_PHOTO); } 
0
source

Source: https://habr.com/ru/post/1339991/


All Articles