E/BitmapFactory: 无法解码流: java.io.FileNotFoundException for (No such file or directory)

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException for (No such file or directory)

我尝试在同一 Activty 中打开图库中的图像并在 Imageview 中显示。我添加了 WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGEINTERNET 等权限。当我 select 来自画廊的图像和 return activity 它显示这种类型的错误

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)

这是我的代码:-

  1. 打开图库的按钮单击事件:-

    btn?.setOnClickListener(object : View.OnClickListener { 覆盖乐趣 onClick(arg0: View) {

            val i = Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
    
            startActivityForResult(i, RESULT_LOAD_IMAGE)
        }
    })
    
  2. 这用于在Imageview中显示图像:-

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

    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))
    
    }
    

    }

您的 query() 不需要 return 您可以使用的文件系统路径。

相反,将您的 Uri 传递给您最喜欢的图像加载库(Glide、Picasso 等)。他们可以将图像加载到您的 ImageView 中,在后台线程上完成所有 I/O。