【问题标题】:Displaying Image from firebase cloud firestore by download URL [duplicate]通过下载 URL 显示来自 firebase cloud firestore 的图像 [重复]
【发布时间】:2019-11-16 15:17:44
【问题描述】:

我正在制作一个 Android 应用程序,其中我有一些用户帐户并与每个用户帐户相关联的是一些数据。该数据还具有与存储在 Firebase 存储中的图像相关联的下载 URL。

我知道如何从 Firebase Cloud Firestore 中检索数据,因此我将在应用程序中检索下载 url 的值 逐行

FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference user = db.collection("School").document("Accouts").collection("students").document(name);

        user.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot doc = task.getResult();
                    String img_url = (String) doc.get("image");
                }
            }
        });

但我不知道如何加载该图像并在 ImageView 中显示

我想在不使用外部库的情况下做到这一点

数据库是

【问题讨论】:

  • 您可以使用 Glide 或 Picasso 或任何其他库来显示它。
  • @Ticherhaz 没有这些库有没有简单的方法
  • 使用 Cloud Firestore 或 Cloud Storage 并不重要。您仍然只需要将 URL 加载到 ImageView 中,这是一个真正已解决多次的老问题。

标签: android firebase google-cloud-firestore firebase-storage


【解决方案1】:

我正在使用 Glide。 https://github.com/bumptech/glide.

ImageView imageView = findViewById(R.id.image_view);

Glide.with(getApplicationContext())
     .load(img_url)
     .into(imageView);

【讨论】:

    【解决方案2】:

    您可以使用Glide 来加载您的图片。

    Glide 是一个快速高效的 Android 开源媒体管理和图像加载框架,它将媒体解码、内存和磁盘缓存以及资源池封装到一个简单易用的界面中。

    使用方法:

    ImageView imageView = (ImageView) findViewById(R.id.your_image_view);
    
    Glide.with(this)
    .load("your URL goes here")
    .into(imageView);
    

    【讨论】:

      【解决方案3】:

      您可以使用 Picasso

      加载图片

      将依赖项添加到您的 app/build.gradle

      implementation 'com.squareup.picasso:picasso:2.71828'
      

      然后你可以像下面这样加载你的图片

      Picasso.get().load(yourImageUrl).into(imageView);
      

      更多信息请查看Piccasso

      【讨论】:

      • picasso 有很多缺陷。如果你关注docs 并且firebase 会过期 url 令牌所以每次它都需要撤销时会更好
      • 我从去年开始使用它,它没有给我批评它的机会。每个人都有自己的喜好
      • 我并不是说它不好或有什么问题,但如果您知道我的意思,获取 url 和其他内容以显示图像需要时间。上面的问题是通过使用存储引用获取图像url,然后如果使用存储引用显示图像会更好更快地显示图像
      猜你喜欢
      • 2020-08-15
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2021-06-06
      • 2019-07-08
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多