2
votes

I try to open image from gallery and display in Imageview in same Activty . I add permissions like WRITE_EXTERNAL_STORAGE , READ_EXTERNAL_STORAGE , INTERNET. when i select image from gallery and return activity it display this type of error

09-04 15:02:57.161 31642-31642/com.androidtutorialpoint.qrcodescanner E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/Pictures/Screenshots/Screenshot_20180816-160721.png (No such file or directory)

Here my code :-

  1. Button click event for open gallery :-

    btn?.setOnClickListener(object : View.OnClickListener { override fun onClick(arg0: View) {

            val i = Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
    
            startActivityForResult(i, RESULT_LOAD_IMAGE)
        }
    })
    
  2. This for use display image in Imageview:-

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

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
        val selectedImage = data.data
        val filePathColumn = arrayOf(MediaStore.Images.Media.DATA)
    
        val cursor = contentResolver.query(selectedImage!!,
                filePathColumn, null, null, null)
        cursor!!.moveToFirst()
    
        val columnIndex = cursor.getColumnIndex(filePathColumn[0])
        val picturePath = cursor.getString(columnIndex)
        cursor.close()
    
        val imageView = findViewById(R.id.imageView) as ImageView
        imageView.setImageBitmap(BitmapFactory.decodeFile("file://" + picturePath))
    
    }
    

    }

1
you have Uri selectedImage - just use it in your ImageViewpskink
how can i add this @pskinkmalik
by reading ImageView official documentationpskink
ok ok thank you for it@pskinkmalik
its not working @pskinkmalik

1 Answers

0
votes

There is no requirement for your query() to return a filesystem path that you can use.

Instead, pass your Uri to your favorite image-loading library (Glide, Picasso, etc.). They can load the image into your ImageView, doing all of the I/O on a background thread.