0

I have ImageView with image choosen by user from gallery:

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

and:

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

Becouse, I need to save Uri of this image to use it later I've created String ImageURI which value is assigned to String Value of imageUri (as you can see above) But when I try to load this image to imageview;

ImageView.setImageUri(ImageURI);

image doesn't load. I don;t get any error. Sample Uri which I was using: content://com.miui.gallery.open/raw/%2storage%2Femulated%2F0%2FDCIM%2Camera%2FIMG_20190521_005650.jpg

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Szymek G
  • 31
  • 6
  • You do not have access to the content later: https://commonsware.com/blog/2016/08/10/uri-access-lifetime-shorter-than-you-might-think.html – CommonsWare May 24 '19 at 20:26
  • Aren't this permissions enought? ` ` – Szymek G May 24 '19 at 20:31
  • No, because you do not have a file. You have a `content` `Uri` from the MIUI gallery app on that device. – CommonsWare May 24 '19 at 20:33
  • maybe duplicate of [this](https://stackoverflow.com/questions/21388586/get-uri-from-camera-intent-in-android) – kAvEh May 24 '19 at 20:43
  • But even if I add: `intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);' the images doesn't show – Szymek G May 24 '19 at 21:07

0 Answers0