【发布时间】:2020-06-05 05:57:10
【问题描述】:
我正在尝试从 Firebase Storage 下载我的 AsyncTask 中的文件,如下所示:
static class DownloadFileFromFireBase extends AsyncTask<String, Void, Boolean> {
File file;
String fileName;
boolean downloadStatus = false;
public DownloadFileFromFireBase(Context context,String fileName, File file){
this.fileName = fileName;
this.file = file;
}
protected Boolean doInBackground(String... urls) {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
StorageReference dataRef = storageRef.child(fileName);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
dataRef.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d(TAG,"File Downloaded");
downloadStatus = true;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.d(TAG,"File Download Failed");
downloadStatus = false;
}
});
return downloadStatus;
}
protected void onPostExecute(InputStream contentsInputStream) {
//TODO:
}
}
我的程序既没有进入 addOnSuccessListener 也没有进入 addOnFailureListener 监听器(Logcat 中没有打印日志)
我暂时将我的 Firebase 规则设置如下:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read: if request.auth == null;
}
}
}
我正在调用我的 AsyncTask,如下所示:
boolean status = new DownloadFileFromFireBase(getContext(), contentsJsonFile).execute("").get();
我存储在 Firebase 存储中的文件可以通过网络浏览器访问(我没有登录/隐身模式)。 我的模拟器和设备正在使用最新的 Google 服务。 甚至在我的项目中实现的 Firebase Storage API 也是最新的(19.1.1)。
我不确定这里出了什么问题。 任何帮助,将不胜感激!谢谢。
【问题讨论】:
标签: java android firebase firebase-storage