简体   繁体   中英

Cropping image on Kitkat is not working

I've written an application in which the user can choose an image from gallery and crop it. It works fine on API<19 but its not working fine on API 19+.

This is my code :

if (Build.VERSION.SDK_INT <19){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser (intent, getResources().getString(R.string.choosephototo)),CAMERA_CAPTURE);
    }
    else {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("image/*");
        startActivityForResult(intent, CAMERA_CAPTURE);
    }

@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 == GALLERY_KITKAT_INTENT_CALLED && resultCode == RESULT_OK) {
        picUri = data.getData();

        final int takeFlags = data.getFlags()
                & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        getContentResolver().takePersistableUriPermission(picUri, takeFlags);

        String id = picUri.getLastPathSegment().split(":")[1];
        final String[] imageColumns = {MediaStore.Images.Media.DATA};
        final String imageOrderBy = null;
        Uri uri = getUri();
        String selectedImagePath = "path";

        Cursor imageCursor = managedQuery(uri, imageColumns,
                MediaStore.Images.Media._ID + "=" + id, null, imageOrderBy);

        if (imageCursor.moveToFirst()) {
            selectedImagePath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        }
        picUri = Uri.parse(selectedImagePath);

        performCrop();
    }

} On android Kitkat it returns the corrent URI of the image, I logged it and is an URI like this: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20.jpg

I think this URI is correct, then it asks for an intent to show cropping application. I have two applications and I have selected both and none of them works.

The cropping application crashes after that.

What is wrong with my code?

 private void picPhoto() {
    Intent pickIntent = new Intent();
    if (Build.VERSION.SDK_INT < 19) {
        pickIntent.setType("image/jpeg");
        pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    } else {
        pickIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
        pickIntent.setType("image/jpeg");
    }
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory());
    takePhotoIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);

    String pickTitle = "Select or take a new Picture";
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);

    chooserIntent.putExtra
            (
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[]{takePhotoIntent}
            );

    startActivityForResult(chooserIntent, PICK_IMAGE);
}

All features will work Api >14

 @TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == PICK_IMAGE && data != null && data.getData() != null) {
            Uri _uri = data.getData();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                String wholeID = DocumentsContract.getDocumentId(_uri);
                // Split at colon, use second item in the array
                String id = wholeID.split(":")[1];
                String[] column = {MediaStore.Images.Media.DATA};
                // where id is equal to
                String sel = MediaStore.Images.Media._ID + "=?";
                Cursor cursor = getActivity().getContentResolver().
                        query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                column, sel, new String[]{id}, null);

                String filePath = "";
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(columnIndex);
                }
                cursor.close();
                croppedPath = filePath;
            } else {
                //User had pick an image.
                Cursor cursor = getActivity().getContentResolver().query(_uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
                cursor.moveToFirst();
                //Link to the image
                final String imageFilePath = cursor.getString(0);
                croppedPath = imageFilePath;
                cursor.close();
            }
            if (croppedPath != null) {
                cropIntent(croppedPath, 5);
            }
        } else if (requestCode != 4 && requestCode == PICK_IMAGE && data == null) {
            cropIntent(getFileDirectory().getPath(), 4);
        } else if (requestCode == 4) {
            editPhoto1.setImageBitmap(null);
            if (bmp != null) {
                bmp.recycle();
            }
            setImagePicked(getFileDirectory().getPath(), 0);
        } else if (requestCode == 5) {
            if (croppedPath != null) {
                editPhoto1.setImageBitmap(null);
                if (bmp != null) {
                    bmp.recycle();
                }
                setImagePicked(getFileDirectory().getPath(), 0);
                croppedPath = null;
            }
        }
    }


    super.onActivityResult(requestCode, resultCode, data);
}

Crop method

private void cropIntent(String fileToCrop, int requestCode) {
    Uri filePath = Uri.fromFile(new File(fileToCrop));
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    //indicate image type and Uri
    cropIntent.setDataAndType(filePath, "image/*");
    cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFileDirectory());
    //set crop properties
    cropIntent.putExtra("crop", "true");
    //indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    //indicate output X and Y
    cropIntent.putExtra("outputX", 256);
    cropIntent.putExtra("outputY", 256);
    //retrieve data on return
    cropIntent.putExtra("return-data", true);
    startActivityForResult(cropIntent, requestCode);
    //start the activity - we handle returning in onActivityResult
}

Get file directory to store

 private Uri getFileDirectory() {
        File image = new File(Util.getPath(), "MY_PROFILE_PIC.jpg");
        return Uri.fromFile(image);
    }

or use android crop library

https://github.com/jdamcd/android-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