java - 从 Firestore 获取所有图像 url

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

我正在尝试使用 Firestore。我需要获取存储在 Firestore 中的文件夹中的所有图像网址。我目前正在关注This .

使用时:

db.collection("images")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Log.d(TAG, document.getId() + " => " + document.getData());
                }
            } else {
                Log.w(TAG, "Error getting documents.", task.getException());
            }
        }
    });

我收到以下错误。

Initially i was getting

Program type already present: com.google.common.annotations.GwtCompatible

问题已解决:

implementation ("com.google.firebase:firebase-firestore:17.0.3"){
    exclude group: 'com.google.guava'
}

java.lang.RuntimeException: Internal error in Firestore (0.6.6-dev).
    at com.google.firebase.firestore.g.zza.zzb(SourceFile:324)
    at com.google.firebase.firestore.g.zzd.run(Unknown Source:2)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NoSuchMethodError: No static method checkArgument(ZLjava/lang/String;I)V in class Lcom/google/common/base/Preconditions; or its super classes (declaration of 'com.google.common.base.Preconditions' appears in /data/app/com.apps.dp-2wjpiDlhe60_b_F7OtfpaQ==/split_lib_dependencies_apk.apk!classes2.dex)
    at com.google.firebase.Timestamp.zza(SourceFile:136)
    at com.google.firebase.Timestamp.<init>(SourceFile:47)
    at com.google.firebase.firestore.d.zzm.<clinit>(SourceFile:12)
    at com.google.firebase.firestore.c.zzav.<init>(SourceFile:24)
    at com.google.firebase.firestore.c.zzat.zzb(SourceFile:104)
    at com.google.firebase.firestore.c.zzi.<init>(SourceFile:125)
    at com.google.firebase.firestore.b.zzf.zza(SourceFile:1215)
    at com.google.firebase.firestore.b.zzh.run(Unknown Source:8)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at com.google.firebase.firestore.g.zza$zza.run(SourceFile:190)
    at java.lang.Thread.run(Thread.java:764)
    07-26 09:56:29.396 25793-25793/com.apps.dp E/UncaughtException: 
    java.lang.RuntimeException: Internal error in Firestore (0.6.6-dev).
    at com.google.firebase.firestore.g.zza.zzb(SourceFile:324)
    at com.google.firebase.firestore.g.zzd.run(Unknown Source:2)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NoSuchMethodError: No static method checkArgument(ZLjava/lang/String;I)V in class Lcom/google/common/base/Preconditions; or its super classes (declaration of 'com.google.common.base.Preconditions' appears in /data/app/com.apps.dp-2wjpiDlhe60_b_F7OtfpaQ==/split_lib_dependencies_apk.apk!classes2.dex)
    at com.google.firebase.Timestamp.zza(SourceFile:136)
    at com.google.firebase.Timestamp.<init>(SourceFile:47)
    at com.google.firebase.firestore.d.zzm.<clinit>(SourceFile:12)
    at com.google.firebase.firestore.c.zzav.<init>(SourceFile:24)
    at com.google.firebase.firestore.c.zzat.zzb(SourceFile:104)
    at com.google.firebase.firestore.c.zzi.<init>(SourceFile:125)
    at com.google.firebase.firestore.b.zzf.zza(SourceFile:1215)
    at com.google.firebase.firestore.b.zzh.run(Unknown Source:8)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at com.google.firebase.firestore.g.zza$zza.run(SourceFile:190)
    at java.lang.Thread.run(Thread.java:764)

过去 6 小时我一直在努力解决这个问题。我可以得到一些帮助吗?我还有一个问题是否可以使用 android 中的 Firestore APi 从文件夹中获取所有图像网址。或者我先将这些 URls 保存在 Firebase 数据库中?已经查看了一些关于此的帖子 Like this one这表明这是不可能的。

最佳答案

根据您的最后评论,请注意,为了检索 Cloud Storage for Firebase 中托管的文件夹内的所有文档,您需要知道路径下载URL。无法在不知情的情况下简单地从文件夹中获取所有文件。

要解决此问题,我建议在上传任何文件时,将下载 URL 存储到类似 Firebase Real-time Database 的数据库中。或新的Cloud Firestore 。请参阅下面获取下载 URL 的简单示例。

storageRef.child("YourFolderName").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for "YourFolderName/YourFile.pdf"
        // Add it to your database
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

最后,只需从 Firebase 实时数据库查询 url 节点或从 Cloud Firestore 查询 url 集合,并在需要的地方使用它们。

关于java - 从 Firestore 获取所有图像 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51531093/

相关文章:

Java 字符串池测试不起作用

android - 使用本地 js/css 加载网站

ios - Firebase 插入具有 2 个关键属性的数据

ios - firebase 配置崩溃

java - 如何在二维数组中查找峰值中停止Java递归

java - 使用Java代码创建Hibernate表索引

android - 错误 : [Dagger/MissingBinding] Map<Class<? 扩展 ViewModel>,如果没有 @Provides-annotated 方法,就无法提供 Provider<ViewModel>>

Android:以编程方式添加的按钮中的中心文本

android - 单击按钮 View 后,recyclerview 的两个项目都会更新

java - FirebaseListAdapter 不为聊天应用程序推送单个项目 - Firebase-Ui 3.1