简体   繁体   中英

android-image cropping from gallery not working properly

I've an form that user can choose image from gallery or take an image . It works fine when user take an image but it's not working properly when user choose an image from gallery and then I ask for an intent for cropping the image .

On emulator cropping image from gallery works find but on 2 phones that i've tested , when I choose the cropping application , the cropping application crashes or if it not crached , it doesn't work and show the image on imageView .

This is my code :

    final int PIC_CROP = 2;
    final int CAMERA_CAPTURE = 1;
    final int PICK_FROM_FILE = 3;
    private Uri picUri;

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_CAPTURE && resultCode == RESULT_OK) {

            picUri = data.getData();// get image URI
            performCrop();
        } else if (requestCode == PIC_CROP) {// crop and compress image
            if (resultCode != RESULT_OK)
                return;
            Bundle extras = data.getExtras();
            Bitmap thePic = extras.getParcelable("data");
            FileOutputStream out = null;
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            double width = thePic.getWidth();
            double height = thePic.getHeight();
            double ratio = 650 / width;
            int newheight = (int) (ratio * height);
            thePic = Bitmap.createScaledBitmap(thePic, 650, newheight, true);
            try {
                out = new FileOutputStream(file);
                thePic.compress(Bitmap.CompressFormat.JPEG, 295, out);
                thePic.compress(Bitmap.CompressFormat.JPEG, 295, bao);

                byte[] ba = bao.toByteArray();
                switch (whichimg) {
                case 1:
                    simg1 = Base64.encodeBytes(ba);
                    break;
            } catch (Exception e) {

            }

        }
    }

if (item == 0) {// take photo
                    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(captureIntent, CAMERA_CAPTURE);
                    alert.dismiss();
                } else if (item == 1) {// photo from gallery
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(
                            Intent.createChooser(intent, (getString(R.string.choosephototo))),
                            CAMERA_CAPTURE);
                    alert.dismiss();
                }

this is the cropping codes :

private void performCrop() {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "image/*");
        cropIntent.putExtra("crop", "true");
        //cropIntent.putExtra("aspectX", 1);
        //cropIntent.putExtra("aspectY", 1);
        //cropIntent.putExtra("outputX", 356);
        //cropIntent.putExtra("outputY", 256);
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, PIC_CROP);
    } catch (ActivityNotFoundException anfe) {
        MyToast.makeText(NewAdd2.this, DariGlyphUtils.reshapeText(getString(R.string.devicecouldcop)));
    }
}

Could you help me ? Why it is not working fine ?

thanks

Try Below link for your Question, it will Work

may Problem in your performCrop() Method

Link For Crop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM