ImageView加载ImageUri

时间:2019-05-24 20:24:09

标签: android imageview uri

我有ImageView的图片是由用户从图库中选择的:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);

和:

@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        try {
            final Uri imageUri = data.getData();
            final InputStream imageStream = getContentResolver().openInputStream(imageUri);
            final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
            ImageView img = findViewById(R.id.img);
            img.setImageBitmap(selectedImage);
            //
            ImageURI = Uri.parse(String.valueOf(imageUri));

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }else {
        Toast.makeText(getApplicationContext(), "You haven't picked Image",Toast.LENGTH_LONG).show();
    }
}

因为,我需要保存此图像的Uri以便以后使用,我创建了String ImageURI,该值已分配给imageUri的字符串值(如上所示) 但是当我尝试将此图像加载到imageview时;

ImageView.setImageUri(ImageURI);

图像未加载。我没有任何错误。 我正在使用的示例Uri: content://com.miui.gallery.open/raw/%2storage%2Femulated%2F0%2FDCIM%2Camera%2FIMG_20190521_005650.jpg

0 个答案:

没有答案