Image View realizes image clipping and display

Image View realizes image clipping and display
First, set the button and an Image View in the layout layout

        android:text="Select pictures" />

        android:text="Select pictures for clipping" />
    <!-- Information for displaying pictures -->

        android:layout_height="wrap_content" />

Write code on Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 private Button selectImageBtn, cutImageBtn;
    private ImageView imageView;
    // Declare two static integer variables, mainly for the return flag of intent
    private static final int IMAGE_SELECT = 1;// Select pictures
    private static final int IMAGE_CUT = 2;// Cut pictures
    protected void onCreate(Bundle savedInstanceState) {
        selectImageBtn = (Button) findViewById(;
        cutImageBtn = (Button) findViewById(;
        imageView = (ImageView) findViewById(;
        // Registered Monitoring Event

Implementation of OnClickListener and Method of Setting up Clipping Pictures

    public void onClick(View v) {
        switch (v.getId()) {
                //How to extract the picture library of mobile phone and select pictures
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//Open the Photo Gallery of Mobile Phone
                startActivityForResult(intent, IMAGE_SELECT);

                Intent intent2 = getImageClipIntent();
                startActivityForResult(intent2, IMAGE_CUT);

private Intent getImageClipIntent() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);//No URL specified
        //In order to realize the clipping of pictures, it is necessary to set the attributes and sizes of pictures.
        intent.setType("image/*");//Get any image type Set an explicit MIME data type. Each MIME type consists of two parts. The first is a large category of data, such as audio, image, etc., and the latter defines a specific category.
        intent.putExtra("crop", "true");//Slide Select Picture Area
        intent.putExtra("aspectX", 1);//Represents the effect of a ratio of 1:1 for a clipping box
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 80);//Specify the size of the output picture
        intent.putExtra("outputY", 80);
        intent.putExtra("return-data", true);//Return value
        return intent;

If you want to get the data returned after the closure of the newly opened Activity in Activity, you need to open the new Activity using the Start Activity ForResult (Intent intent, int request Code) method provided by the system. After the closure of the new Activity, the data will be returned to the front Activity. In order to get the data returned, you must override the onActivity Result (int request Code) in the previous Activity. Int resultCode, Intent data) method

When the new Activity is closed, the data returned by the new Activity is transferred through Intent. The Android platform calls the onActivityResult() method of the previous Activity, and the Intent storing the returned data is passed in as the third input parameter. The data returned by the new Activity can be retrieved by using the third input parameter in the onActivityResult() method.

If data or results need to be returned, start Activity ForResult (Intent intent, intrequestCode) is used, and the value of requestCode is self-defined to identify the target activity of the jump.

Overlay onActivityResult Method

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            //Processing pictures and displaying them according to the screen size of the mobile phone
            if (requestCode == IMAGE_SELECT) {
                Uri uri = data.getData();//Path to Get Pictures
                Display display = getWindowManager().getDefaultDisplay();
                Point point = new Point();
                int width = point.x;//Get the width of the screen
                int height = point.y ;//Screen height

                try {
                    //The class that implements image clipping is an anonymous inner class
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = false;
                    //Matching the width and height of the picture to the screen of the mobile phone
                    Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);

                    //If greater than 1, the height of the picture is greater than the height of the mobile screen.
                    int hRatio = (int) Math.ceil(options.outHeight / (float) height);//(int) Math.ceil is down-rounding
                    //If greater than 1, the width of the picture is larger than the width of the mobile screen.
                    int wRatio = (int) Math.ceil(options.outWidth / (float) width);
                    //If hRatio or wRatio is greater than 1, zoom the image to 1/radio size and 1/radio^2 pixels
                    if (hRatio > 1 || wRatio > 1) {
                        if (hRatio > wRatio) {
                            options.inSampleSize = hRatio;
                        } else {
                            options.inSampleSize = wRatio;
                        bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
                        //If hRatio and wRatio are 0, direct output

                } catch (Exception e) {

                //Represents clipped pictures
            } else if (requestCode == IMAGE_CUT) {
                Bitmap bitmap = data.getParcelableExtra("data");
options.inJustDecodeBounds = false/true; 

Let's parse a picture. If it's too big, it's OOM. We can set the compression ratio in SampleSize, but how much is the problem. So we can parse the picture in two steps. The first step is to get the width of the picture. Here we set Options. In JustDecodeBounds = true. At this time, the bitmap of decode is null, just the width of the picture. Put it in Options.

Then the second step is to set the appropriate compression ratio inSampleSize, inSampleSize to the original 1/ratio, then get the appropriate Bitmap.

Set options.inJustDecodeBounds = false; re-read the picture bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);

Tags: Android Mobile

Posted on Thu, 04 Apr 2019 18:54:29 -0400 by winsonlee