如何使用 Glide 从本地存储加载图像
How to load image from local storage using Glide
我想从图片目录加载到 imageView 一张图片。使用此代码我总是会出错,但是文件存在。
这是一个非常原始的例子。更大的项目需要它,但我想让它尽可能简单。
build.gradle(模块 - 代码的唯一重要部分):
android {
compileSdk 32
defaultConfig {
minSdk 28
targetSdk 32
versionCode 1
dependencies {
implementation "com.github.bumptech.glide:glide:4.13.0"
}
glide 4.13.0应该是最新版本
AndroidActivity.xml(仅重要部分):
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 999) // to get permission read from external storage
val image1: ImageView = findViewById(R.id.imageView)
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()).list()?.get(0)
Glide.with(this)
.asBitmap()
.load(file)
.into(image1)
}
当我调试我的代码时,我看到变量 file
存在并且它的名称是 IMG_20220317_085955.jpg
错误信息:
java.io.FileNotFoundException(open failed: ENOENT (No such file or directory))
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
I/Glide: Root cause (1 of 3)
java.io.FileNotFoundException: /IMG_20220317_085955.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:492)
为了获得我使用的完整路径
val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).list()
?.get(0)
.toString()
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
将生成路径 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).list()?.get(0)
获取第一张图像(如果存在)
我想从图片目录加载到 imageView 一张图片。使用此代码我总是会出错,但是文件存在。
这是一个非常原始的例子。更大的项目需要它,但我想让它尽可能简单。
build.gradle(模块 - 代码的唯一重要部分):
android {
compileSdk 32
defaultConfig {
minSdk 28
targetSdk 32
versionCode 1
dependencies {
implementation "com.github.bumptech.glide:glide:4.13.0"
}
glide 4.13.0应该是最新版本
AndroidActivity.xml(仅重要部分):
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 999) // to get permission read from external storage
val image1: ImageView = findViewById(R.id.imageView)
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()).list()?.get(0)
Glide.with(this)
.asBitmap()
.load(file)
.into(image1)
}
当我调试我的代码时,我看到变量 file
存在并且它的名称是 IMG_20220317_085955.jpg
错误信息:
java.io.FileNotFoundException(open failed: ENOENT (No such file or directory))
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
I/Glide: Root cause (1 of 3)
java.io.FileNotFoundException: /IMG_20220317_085955.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:492)
为了获得我使用的完整路径
val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).list()
?.get(0)
.toString()
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
将生成路径 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).list()?.get(0)
获取第一张图像(如果存在)