繁体   English   中英

Android从Intent获取裁剪图像的URI

[英]Android get URI of cropped image from Intent

我正在尝试从Android相机捕获图像/从图库中选择图像,然后在对其执行其他操作之前对其进行裁剪。 我在找回裁剪图像的URI时遇到麻烦。 非常感谢您提供有关裁剪后如何取回图像URI的任何帮助!

以下代码与我的onActivityResult和执行裁剪的函数有关

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE || requestCode == REQUEST_IMAGE_CAPTURE) {
            if(!IS_CAMERA_USED) {
                selectedImageUri = data.getData();
            }
            performCrop();
        }
        else if(requestCode == CROP_IMAGE){
            selectedImageUri = data.getData();
            onSelectedImageResult();
        }
    }
}



    public void performCrop() {
    // take care of exceptions
    Display display = getWindowManager().getDefaultDisplay();
    try {
        // call the standard crop action intent (the user device may not
        // support it)
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(selectedImageUri, "image/*");
        // set crop properties
        cropIntent.putExtra("crop", "true");
        // indicate aspect of desired crop
        cropIntent.putExtra("aspectY", 1);
        if(IS_PROFILE_PICTURE) {
            cropIntent.putExtra("aspectX", 1);
        }
        else {
            cropIntent.putExtra("aspectX", 2);
        }
        // indicate output X and Y
        cropIntent.putExtra("outputX", display.getWidth());
        cropIntent.putExtra("outputY", display.getHeight());
        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, CROP_IMAGE);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        Toast toast = Toast
                .makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
        toast.show();
    }
}

问题在于selectedImageUri = data.getData() ,其中data.getData()在完成裁剪后返回null。 如何获取裁剪图像的URI? 我不想获取data.getExtras.getParcelable("data")因为这会返回缩略图并破坏图像分辨率。

提前致谢!

您可以使用以下代码获取图像位图:

if (requestCode == CROP_IMAGE) {
    if (data != null) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap selectedBitmap = extras.getParcelable("data");

            //imgView.setImageBitmap(selectedBitmap);
        }
    }
}

我认为URI是不可能的,因为裁剪后的图像没有保存在存储器中。

感谢您的所有答复,但是我发现了一种更易于合并和使用的方法,可以避免由于com.android.camera.action.CROP类而引起的所有麻烦,可以在此处找到

我认为您应该将裁剪后的位图保存到存储中,然后使用裁剪后的位图的URI对其执行一些操作! 我知道这不是专业工作,但至少应该做

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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