Android calls the system Intent for image selection and cropping

Call the system Intent for image selection and cropping

Steps to achieve:
1. Call the picture in the album to crop and then display it.
2. Call the camera function to get the picture cropped and displayed.

Call the picture in the photo album to crop and then display

The first step is to jump to the system album to get pictures

   public static final int VALUE_PICK_PICTURE = 2;
    private void selectPicFromLocal() {
       Intent intent = new Intent(Intent.ACTION_PICK, null);
    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
       startActivityForResult(intent, VALUE_PICK_PICTURE);
    }

  1. new Intent(Intent.ACTION_PICK, null); This method can be called to let the user select the photo picker. The system selects all available programs for the user to choose from
  2. intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")If you want to limit the type of images uploaded to the server, you can directly write such as: "type of image/jpeg, image/png, etc."

The second step onActivityResult receives the selected image information

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == RESULT_OK) {
           switch (requestCode) {
               case VALUE_PICK_PICTURE: // If it is obtained directly from the album
                   startPhotoZoom(data.getData());//Get the Uri of the selected picture
                   break;
           }
       }
    }

Get the Uri of the selected picture through the data.getData() method. It should be noted here that if the user does not select any picture after the jump and presses the return key, then the data is null, which will cause a null pointer exception . That's why we judge resultCode == RESULT_OK. If the user pressed the back key, here resultCode == RESULT_CANCELED.

Step 3 Crop the selected image

private static final String IMAGE_FILE_LOCATION = "file:///" + Environment.getExternalStorageDirectory().getPath() + "/temp.jpg";
private Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);

public void startPhotoZoom(Uri uri) {
   Intent intent = new Intent("com.android.camera.action.CROP");
   intent.setDataAndType(uri, "image/*");
   // The following crop=true is set to set the displayed VIEW in the open Intent to be cropped
    intent.putExtra("crop", "true");
    //This parameter can not be set to specify the aspect ratio of the cropping area
    intent.putExtra("aspectX", 2);
    intent.putExtra("aspectY", 1);
    //This parameter is set to the size of your imageView
    intent.putExtra("outputX", 600);
    intent.putExtra("outputY", 300);
    intent.putExtra("scale", true);
    //Whether to return the bitmap object
    intent.putExtra("return-data", false);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());//Format of the output image
    intent.putExtra("noFaceDetection", true); // avatar recognition
   startActivityForResult(intent, 3);
}

Among them aspectX/aspectY is the aspect ratio, which can be set to 16:9 or 1:1. If it is not set, the size of the clipping will be determined by the user, that is, the aspect ratio of the clipping area is uncertain.

outputX/outputY is the width and height of the output picture after the intent is cropped. This property can be used when we need to get a picture of the same size as imageView. If the cropped picture is smaller than the value we set, the cropping is completed. Will get an image the same size as our selected area. If the image we want to crop is relatively large, the output after cropping will be the width and height values ​​we set. So to sum up, we just need to set the value to the pixel value we want to get.

There is also a very important attribute, return-data This is the attribute that determines what data we receive in onActivityResult. If set to true, data will return a bitmap. If we set it to false, the image will be saved to the local and Return the corresponding uri, of course, this uri has to be set by ourselves. After the system is finished cropping, it will save the cropped image at the uri address we set. We just need to call the uri directly to set the image after the cropping is complete, and that's it.

The following is the complete code to call the system album to crop the picture:

public class PictureActivity extends AppCompatActivity {

    private static final String TAG = PictureActivity.class.getSimpleName();
    private static final int CHOOSE_PICTURE = 1;
    private static final int CROP_PICTURE = 2;
    @BindView(R.id.btn_select_from_local)
    Button mBtnSelectFromLocal;
    @BindView(R.id.iv_local)
    ImageView mIvLocal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_picture);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_select_from_local)
    public void onClick() {
        selectFromLocal();
    }


    private void selectFromLocal() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, CHOOSE_PICTURE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, resultCode + "");
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CHOOSE_PICTURE:
                    startPhotoZoom(data.getData());
                    break;
                case CROP_PICTURE: // Get the cropped picture
                    try {
                        mIvLocal.setImageBitmap(BitmapFactory.decodeStream( getContentResolver().openInputStream(imageUri) ));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace ();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    private static final String IMAGE_FILE_LOCATION = "file:///" + Environment.getExternalStorageDirectory().getPath() + "/temp.jpg";
    private Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);

    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // The following crop=true is set to set the displayed VIEW in the open Intent to be cropped
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);

        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 1);

        intent.putExtra("outputX", 600);
        intent.putExtra("outputY", 300);

        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection

        startActivityForResult(intent, CROP_PICTURE);
    }
}
Call the system camera to take a picture, then crop the picture

The difference from selecting from an album lies in the way to obtain pictures, and the subsequent cropping operations are basically similar.

The first step is to call the system camera to take a picture

In order to record different Uri generation methods, the File method is used, which is essentially the same as the String spliced ​​when using the album selection above.

private File  mFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "temp.jpg");

private void selectPicFromCamera() {
   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
   // The following sentence specifies the path of the photo storage after calling the camera to take a photo
   intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
   startActivityForResult(intent, PICK_CAMERA);
}

The second step is to call the cropping method

case added in onActivityResult

case PICK_CAMERA:
     startPhotoZoom(Uri.fromFile(mFile));
     break;
  public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // The following crop=true is set to set the displayed VIEW in the open Intent to be cropped
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);

        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 1);

        intent.putExtra("outputX", 600);
        intent.putExtra("outputY", 300);

        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection

        startActivityForResult(intent, CROP_PICTURE);
    }
It should be noted here that if the location of the picture we set to save the picture after taking a picture is the intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);same as the saving address after cropping, then after the cropping is completed, the original picture will be replaced.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326282917&siteId=291194637