Rebuild Uri from given Uri path

36 Views Asked by At

I have an android application in which I'm storing the decoded path of the uri (which is pointing to an image) as an attribute. I was wondering if there was a way to rebuild the Uri so I could set the image using:

imgView.setImageDrawable(Drawable.createFromStream(
                    getContentResolver().openInputStream("Uri.rebuildFromPath(uriPath)"),
                    null));

When I first assign the values this way:

String uriPath = Uri.fromFile(new File("/path/to/img")).getPath();
Bitmap imgBitmap = BitmapFactory.decodeFile(uriPath);
imageView.setImageBitmap(imgBitmap);

It works without a problem. The issue happens with the Uri returned by the PhotoPicker:

ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
            registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), uri -> {
                // Callback is invoked after the user selects a media item or closes the
                // photo picker.
                if (uri != null) {
                    Log.d("PhotoPicker", "Selected URI: " + uri);
                    Log.d("IMG_PATH", uri.getEncodedPath());
                    uriPath = uri.getPath();
                    Bitmap imgBitmap = BitmapFactory.decodeFile(uriPath);
                    imageView.setImageBitmap(imgBitmap);

                } else {
                    Log.d("PhotoPicker", "No media selected");
                }
            });
0

There are 0 best solutions below