Crop an image when selected from gallery in android

后端 未结 6 548
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 08:51

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 sele

6条回答
  •  北海茫月
    2020-12-02 08:56

    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!! :)

提交回复
热议问题