【问题标题】:Android - Download a zip file from Firebase StorageAndroid - 从 Firebase 存储下载 zip 文件
【发布时间】:2017-07-22 09:19:16
【问题描述】:

我已经实现了一个按钮,可以让我从 Firebase 存储下载一个 zip 文件。

这是我的代码

FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();

.buttonCtaClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //download stuff
                        try {
                            File imageFile = File.createTempFile("Chords_Images", "zip");

                            storageRef.getFile(imageFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                                    Toast.makeText(SplashActivity.this, "file created", Toast.LENGTH_SHORT).show();
                                    //TODO: download audio
                                    startApp();
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(SplashActivity.this, "An error accoured", Toast.LENGTH_SHORT).show();
                                }
                            });
                        } catch (IOException e) {e.printStackTrace();}
                    }
                })

问题是它总是失败并调用FailureListener 怎么会发生这种情况?

这是异常的堆栈跟踪:

获取令牌时出错 java.util.concurrent.ExecutionException: com.google.firebase.FirebaseApiNotAvailableException: firebase-auth is not linked, 请退回到未验证模式。 07-22 11:33:57.991 4272-4417/com.dancam.chords E/StorageException:发生了 StorageException。 用户无权访问此对象

这是我的 Firebase 存储规则

service firebase.storage {
  match /b/chords-d1534.appspot.com/o {
    match /{allPaths=**} {
      allow read: if true; //if request.auth != null;
    }
  }
}

【问题讨论】:

  • 尝试打印Exception e 的文字,并向我们展示您的storageRef
  • 你能发布你的firebase存储规则吗?
  • @sudo 完成,检查一下

标签: android firebase


【解决方案1】:

您以错误的方式下载文件。您对文件的引用应该是:

FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference imageRef = storageRef.child("path/to/file.zip");

并使用它:

imageRef.getFile(imageFile).addOnSuccess...


如果它没有解决问题,让我们看一下错误消息。

com.google.firebase.FirebaseApiNotAvailableException: firebase-auth is not linked,请退回到未经身份验证的模式。 07-22 11:33:57.991 4272-4417/com.dancam.chords

尝试链接firebase-auth 库(将其添加到您的应用级build.gradle):

compile 'com.google.firebase:firebase-auth:11.0.2'

【讨论】:

  • 路径是我的Firebase Storage的根目录,我需要在代码中写什么路径?
  • 然后写filename.zip
  • @Daniele 如果解决方案对您有用,请确保接受答案。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 2017-08-20
相关资源
最近更新 更多