Crop an image when selected from gallery in android

后端 未结 6 560
佛祖请我去吃肉
佛祖请我去吃肉 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 09:13

    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:

    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:

    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 post which is also help you for cropping image in android

提交回复
热议问题