【问题标题】:Is it possible to set image to ImageView from a path (external storage) on Android 10 and higher?是否可以在 Android 10 及更高版本上从路径(外部存储)将图像设置为 ImageView?
【发布时间】:2020-08-15 20:17:31
【问题描述】:

我需要在 ImageView 上显示来自用户手机内存(外部存储)某处的图片的图像。我使用图像绝对路径来实现这一点,它在任何 Android 手机上都运行良好。但在 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 imageview androidx android-glide android-external-storage


    【解决方案1】:

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

     android:requestLegacyExternalStorage="true"
    

    在你的 gradle 文件中设置这个选项(如果不是)

     compileSdkVersion 29
    

    并且在获得内存使用权限后——可以上传图片

    【讨论】:

    • 谢谢,您的解决方案对我有用!但是您知道将来是否会支持使用旧版外部存储,比如说在 Android 11+ 中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 2021-01-09
    • 1970-01-01
    • 2023-03-23
    • 2021-06-07
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多