Deepak V
Deepak V

Reputation: 319

Get Image URI from Gallery and passing it to another activity, cannot see image, get an exception

I have an activity from which I launch the gallery, select an image, and want to display the selected image in another activity. I have referred to the following solution and implemented the same.

How to get Image URI from Gallery?

Though I am able to pass the URI to the next activity, I cannot see anything on the image view. Any help as to where I am going wrong, appreciated.

btn_launch_gallery.setOnClickListener {

            val requestCode = 0
            val launchGalleryIntent = Intent(Intent.ACTION_PICK, 
                                             MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(launchGalleryIntent, requestCode)
        }

My OnActivityResult looks like this, basically implemented the same as given in the example cited above.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        super.onActivityResult(requestCode, resultCode, data)


        if (requestCode === 0 && resultCode === Activity.RESULT_OK ) {
            val selectedImage: Uri? = data?.data
            val picturePath = getRealPathFromURI(
                selectedImage,
                this
            )
            val intent = Intent(this, LogFoodDetail::class.java)
            intent.putExtra("image_from_gallery", picturePath)

            try {
                startActivity(intent)
            }
            catch (e: Exception)
            {
                e.printStackTrace()
                Log.e("Error",e.printStackTrace().toString())
            }
        }
    }

    fun getRealPathFromURI(contentURI: Uri?, context: Activity): String? {
        val projection =
            arrayOf(MediaStore.Images.Media.DATA)
        val cursor = context.managedQuery(
            contentURI, projection, null,
            null, null
        )
            ?: return null
        val column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        return if (cursor.moveToFirst()) {
            // cursor.close();
            cursor.getString(column_index)
        } else null
        // cursor.close();
    }

In my next activity, I getting the intent like this and passing the URI to ImageView. However, I cannot see the image. I get the following error - W/System.err: java.io.FileNotFoundException: No content provider: /storage/emulated/0/DCIM/Camera/***.jpg

val resId = intent.getStringExtra("image_from_gallery")
val imgThumbnail: ImageView = findViewById(R.id.food_thumbnail)
        try{
            val imageStream: InputStream? = contentResolver.openInputStream(Uri.parse(resId))
            val bitmap = BitmapFactory.decodeStream(imageStream)
            imgThumbnail.setImageBitmap(bitmap)
        }
        catch (e: Exception)
        {
          e.printStackTrace()
        }

I see the following image in the next activity:enter image description here

UPDATE: As commented by @blackapps in his answer passing the URI as a string to the next activity on an intent.putExtra() and resolving the URI in the subsequent activity solved it, the updated code in OnActivityResult() is,

...
            val selectedImage: Uri? = data?.data
            val intent = Intent(this, LogFoodDetail::class.java)
            intent.putExtra("image_from_gallery",                                          
                            selectedImage.toString())
            startActivity(intent)

Upvotes: 1

Views: 2186

Answers (2)

Rupam Saini
Rupam Saini

Reputation: 21

You can directly load an local image Uri using:

imgThumbnail.setImageUri(yourUri);

Instead of sending the string path to the activity, you should send the raw uri and then set it directly to the imageView.

Upvotes: 1

blackapps
blackapps

Reputation: 9292

Dont convert a nice uri to a file system path.

   Uri uri = data.getData();

Pass the obtained uri directly to the next activity.

And there you can use it for

   imageView.setImageUri(uri);

Instead of the uri you can also pass the uri as string with uri.toString().

Upvotes: 1

Related Questions