title: Get Image URI from Gallery and passing it to another activity, cannot see image, get an exception
body: <p>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.</p>
  <p><a href="https://stackoverflow.com/questions/32652762/how-to-get-image-uri-from-gallery">How to get Image URI from Gallery?</a></p>
  <p>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.</p>
  <pre><code>btn_launch_gallery.setOnClickListener {
  
              val requestCode = 0
              val launchGalleryIntent = Intent(Intent.ACTION_PICK, 
                                               MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
              startActivityForResult(launchGalleryIntent, requestCode)
          }
  </code></pre>
  <p>My OnActivityResult looks like this, basically implemented the same as given in the example cited above.</p>
  <pre><code>override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  
          super.onActivityResult(requestCode, resultCode, data)
  
  
          if (requestCode === 0 &amp;&amp; resultCode === Activity.RESULT_OK ) {
              val selectedImage: Uri? = data?.data
              val picturePath = getRealPathFromURI(
                  selectedImage,
                  this
              )
              val intent = Intent(this, LogFoodDetail::class.java)
              intent.putExtra(&quot;image_from_gallery&quot;, picturePath)
  
              try {
                  startActivity(intent)
              }
              catch (e: Exception)
              {
                  e.printStackTrace()
                  Log.e(&quot;Error&quot;,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();
      }
  </code></pre>
  <p>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</p>
  <pre><code>val resId = intent.getStringExtra(&quot;image_from_gallery&quot;)
  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()
          }
  </code></pre>
  <p>I see the following image in the next activity:<a href="https://i.stack.imgur.com/3MXru.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3MXru.jpg" alt="enter image description here" /></a></p>
  <p><em><strong>UPDATE:</strong></em>
  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,</p>
  <pre><code>...
              val selectedImage: Uri? = data?.data
              val intent = Intent(this, LogFoodDetail::class.java)
              intent.putExtra(&quot;image_from_gallery&quot;,                                          
                              selectedImage.toString())
              startActivity(intent)
  </code></pre>