【问题标题】:Loading image from firebase storage not loading image using glide using Kotlin从 Firebase 存储加载图像而不是使用 Kotlin 使用 glide 加载图像
【发布时间】:2020-06-28 00:02:49
【问题描述】:

我正在尝试使用 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 存储屏幕截图如下:

我对此进行了更多测试,无论我从哪里提取 URL,Glide 似乎都可以从 HTTPS 加载图像。但是云存储提供的 URL 为 GS://。那么如何将 GS:// 转换为 HTTPS://?

请帮忙。

【问题讨论】:

    标签: kotlin firebase-storage android-glide


    【解决方案1】:

    我想通了。

    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://

    【讨论】:

      【解决方案2】:

      两者的区别:

      GS 网址:

      gs://<your_project_id>.appspot.com/test/test.jpg
      

      HTPPS 网址:

      https://firebasestorage.googleapis.com/v0/b/<your_project_id>.appspot.com/o/test%2Ftest.jpg
      

      【讨论】:

        【解决方案3】:

        这是解决我的问题的过程 -

        第 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>)
        
        

        【讨论】:

          猜你喜欢
          • 2018-09-03
          • 1970-01-01
          • 2020-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-30
          • 1970-01-01
          • 2018-10-26
          相关资源
          最近更新 更多