是否可以从 Android 10 及更高版本的路径(外部存储)将图像设置为 ImageView?

Is it possible to set image to ImageView from a path (external storage) on Android 10 and higher?

我需要在 ImageView 上显示位于用户 phone 内存(外部存储)中某处的图片中的图像。我使用图像绝对路径来实现这一点,它在任何 Android phone 上运行良好。但是在 Android 10 及以上这个策略根本不起作用(加载的图像是透明的)。我知道从 Android 10 Google 开始对外部存储器的可访问性进行了很多更改,但是仍然有办法从外部路径显示图像吗?

我同时使用位图解码和 Glide 从路径设置图像,但在 Android 10+ 上没有任何效果 :(

fun setImageFromPath(imagePath: String, imageView: ImageView){
            val imgFile = File(imagePath)
            if (imgFile.exists()) {
                val bitmap = BitmapFactory.decodeFile(imgFile.absolutePath)
                imageView.setImageBitmap(bitmap)
            }
        }

fun setImageFromPathWithGlide(imagePath: String, imageView: ImageView, context: Context){
            Glide.with(context).asDrawable().load(Uri.fromFile(File(imagePath)))
                    .dontTransform()
                    .into(imageView)
        }

是的,这是可能的。首先将这一行添加到应用程序清单

 android:requestLegacyExternalStorage="true"

在您的 gradle 文件中设置此选项(如果没有)

 compileSdkVersion 29

并且在获得使用访问内存的权限后 - 您可以上传图片