首页 > 解决方案 > 将文件从 Firebase 下载到 Android

问题描述

我已将一些 doc 文件存储在 Firebase 存储中的文件夹“Colleges_names”中。我想在我的安卓应用程序中下载它们。当我单击所需的按钮下载文件时,没有任何反应。

public void onClick(View view) {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl("gs://du-admissions-app.appspot.com");
    StorageReference  islandRef = storageRef.child("COLLEGES-NAMES").child("CLG_BA(ENGLISH).docx");

    File rootPath = new File(Environment.getExternalStorageDirectory(), "file_name");
    if(!rootPath.exists()) {
        rootPath.mkdirs();
    }

    final File localFile = new File(rootPath,"CLG_BA(ENGLISH).docx");

    islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Log.e("firebase ",";local tem file created  created " +localFile.toString());
            //  updateDb(timestamp,localFile.toString(),position);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            Log.e("firebase ",";local tem file not created  created " +exception.toString());
        }
    });

标签: androidfirebasefirebase-storage

解决方案


您无法使用扩展名检索这些文件,您应该首先以编程方式上传文件,获取downloadUrl()上传的文件,然后检索该文件并downloadUrl()下载文件。

我建议您阅读此内容


推荐阅读