Kitkat上的裁剪图像不起作用

时间:2015-07-01 13:18:14

标签: android

我编写了一个应用程序,用户可以从中选择一个图像并裁剪它。它在API< 19上运行良好,但它在API 19+上运行不正常。

这是我的代码:

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();
    }

} 在android Kitkat上,它返回图像的正确URI,我记录它并且是这样的URI: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20.jpg

我认为这个URI是正确的,然后它要求显示裁剪应用程序的意图。我有两个应用程序,我已经选择了两个,但没有一个可以工作。

之后裁剪应用程序崩溃。

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

air_date  | num_commercials
2015-6-30 | 6
2015-6-30 | 3
2015-6-30 | 8
2015-6-30 | 2
2015-6-31 | 9
2015-6-31 | 4
  

所有功能都可以使用Api&gt; 14

 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);
}
  

裁剪方法

 @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);
}
  

获取要存储的文件目录

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
}
  

或使用Android裁剪库

https://github.com/jdamcd/android-crop