从 firebase 存储加载图像而不是使用 Kotlin 使用 glide 加载图像
Loading image from firebase storage not loading image using glide using Kotlin
我正在尝试使用 Kotlin 和 Glide 从 Firebase 存储加载图像。我添加了所有依赖项并应用了插件:
implementation 'com.firebaseui:firebase-ui-storage:6.2.0'
implementation 'com.google.firebase:firebase-storage-ktx:19.1.1'
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.9.0'
apply plugin: 'kotlin-kapt'
我的代码如下:
val storageRef = Firebase.storage.reference
val imageref = storageRef.child("test/test.jpg")
imagetest = findViewById(R.id.imageView5)
Glide.with(this)
.load(imageref)
.into(imagetest)
当 运行 代码时,具有默认图像的图像视图变黑,表明代码正在尝试从 Firebase 存储中检索某些内容。但 imageView 从不填充下载的图像。假设下载确实发生了。
我是不是做错了什么?我的 firebase 存储屏幕截图如下:
我对此进行了更多测试,Glide 似乎可以从 HTTPS 加载图像,无论我从哪里提取 URL。但云存储提供 URL 作为 GS://。那么如何将 GS:// 转换为 HTTPS://?
请帮忙。
我想通了。
imageref = Firebase.storage.reference.child("test/test.jpg")
imageref.downloadUrl.addOnSuccessListener {Uri->
val imageURL = Uri.toString()
imagetest = findViewById(R.id.imageView5)
Glide.with(this)
.load(imageURL)
.into(imagetest)
}
'downloadurl' 语句实际上将 GS:// 转换为 HTTPS://
两者的区别:
GS URL:
gs://<your_project_id>.appspot.com/test/test.jpg
HTTPS URL:
https://firebasestorage.googleapis.com/v0/b/<your_project_id>.appspot.com/o/test%2Ftest.jpg
这是解决我的问题的过程 -
第 1 步 - 转到 build.gradle(app) 并添加这些依赖项
implementation 'com.firebaseui:firebase-ui-storage:4.3.2'
implementation 'com.github.bumptech.glide:glide:4.x.x'
annotationProcessor 'com.github.bumptech.glide:compiler:4.x.x'
第 2 步 -
创建 java 文件
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
第 3 步 - 在您想要的位置设置图像
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://<your-app>/").child("folder"); //add .child(nested_folder) if nested folder occurs
GlideApp.with(context)
.load(storageRef.child(test_image.jpg))
.into(holder.thumbnail);
//that .load() will be load like .load(gs://<your_app/folder/test_image.jpg>)
我正在尝试使用 Kotlin 和 Glide 从 Firebase 存储加载图像。我添加了所有依赖项并应用了插件:
implementation 'com.firebaseui:firebase-ui-storage:6.2.0'
implementation 'com.google.firebase:firebase-storage-ktx:19.1.1'
implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.9.0'
apply plugin: 'kotlin-kapt'
我的代码如下:
val storageRef = Firebase.storage.reference
val imageref = storageRef.child("test/test.jpg")
imagetest = findViewById(R.id.imageView5)
Glide.with(this)
.load(imageref)
.into(imagetest)
当 运行 代码时,具有默认图像的图像视图变黑,表明代码正在尝试从 Firebase 存储中检索某些内容。但 imageView 从不填充下载的图像。假设下载确实发生了。
我是不是做错了什么?我的 firebase 存储屏幕截图如下:
我对此进行了更多测试,Glide 似乎可以从 HTTPS 加载图像,无论我从哪里提取 URL。但云存储提供 URL 作为 GS://。那么如何将 GS:// 转换为 HTTPS://?
请帮忙。
我想通了。
imageref = Firebase.storage.reference.child("test/test.jpg")
imageref.downloadUrl.addOnSuccessListener {Uri->
val imageURL = Uri.toString()
imagetest = findViewById(R.id.imageView5)
Glide.with(this)
.load(imageURL)
.into(imagetest)
}
'downloadurl' 语句实际上将 GS:// 转换为 HTTPS://
两者的区别:
GS URL:
gs://<your_project_id>.appspot.com/test/test.jpg
HTTPS URL:
https://firebasestorage.googleapis.com/v0/b/<your_project_id>.appspot.com/o/test%2Ftest.jpg
这是解决我的问题的过程 -
第 1 步 - 转到 build.gradle(app) 并添加这些依赖项
implementation 'com.firebaseui:firebase-ui-storage:4.3.2'
implementation 'com.github.bumptech.glide:glide:4.x.x'
annotationProcessor 'com.github.bumptech.glide:compiler:4.x.x'
第 2 步 - 创建 java 文件
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
registry.append(StorageReference.class, InputStream.class,
new FirebaseImageLoader.Factory());
}
}
第 3 步 - 在您想要的位置设置图像
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://<your-app>/").child("folder"); //add .child(nested_folder) if nested folder occurs
GlideApp.with(context)
.load(storageRef.child(test_image.jpg))
.into(holder.thumbnail);
//that .load() will be load like .load(gs://<your_app/folder/test_image.jpg>)