Uri转换为位图,然后传输另一个活动

时间:2018-09-16 14:39:29

标签: java android

在提供位图传输另一项活动之后,我一直试图通过uri提供位图?

我找到了几种转换我不成功的uri的方法

Uri imageUri = data.getData();
try {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
    picture.setImageBitmap(bitmap);
} 
catch (IOException e) {
    e.printStackTrace();
}

我尝试实施失败的代码

Uri IMAGE_URI = imageReturned
                InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI);
                Bitmap bitmap= BitmapFactory.decodeStream(image_stream );
                picture.setImageBitmap(bitmap);

那是我尝试过的代码的另一种方式

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

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            picture.setImageURI(resultUri);
        } 
        else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            Toast.makeText(this, "Error Merroor", Toast.LENGTH_SHORT).show();
        }
    }
}

0 个答案:

没有答案