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

问题描述 投票:0回答:1

我需要在ImageView上显示用户手机内存(外部存储)中某处的图片中的图片。我使用图片绝对路径来实现这一目标,并且在任何Android手机上都可以正常使用。但是在Android 10及更高版本上,此策略根本不起作用(加载的图像是透明的)。我知道Google从Android 10对外部存储器的可访问性进行了许多更改,但是仍然可以通过外部路径显示图像吗?

我同时使用位图解码和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 imageview androidx android-external-storage glide
1个回答
0
投票

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

 android:requestLegacyExternalStorage="true"

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

 compileSdkVersion 29

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

© www.soinside.com 2019 - 2024. All rights reserved.