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 :-
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) } })
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)) }
}
Uri selectedImage
- just use it in yourImageView
– pskink