【发布时间】: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