将文件从Firebase下载到Android

时间:2018-05-21 00:05:57

标签: android firebase firebase-storage

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

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());
        }
    });

1 个答案:

答案 0 :(得分:0)

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

我建议您read this