如何将文件从Firebase存储下载到Android的External_Storage [英] How to download a file from Firebase Storage to the External_Storage of Android

查看:169
本文介绍了如何将文件从Firebase存储下载到Android的External_Storage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF和一些doc文件保存在Firebase存储器中.

I have a PDF and some doc files kept in firebase storage.

如何将文件从Firebase存储下载到设备的外部存储?

How can I download the file from Firebase Storage to the External storage of my device?

public void writeExternalStorage() {
    String filename;
    String completepath;
    mref = FirebaseStorage.getInstance().getReference();
    StorageReference filepath = mref.child("MyFiles").child("firstFile.pdf");
    InputStream inputStream = null;
    File file = null;
    FileOutputStream fileOutputStream = null;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        try {
            file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + "/TestPurpose");
            //  Log.d("PATH", file.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我想将Firstfile.pdf文件下载到External_storage的文档/TestPurpose/文件夹中.怎么做?

I want to download the Firstfile.pdf file to the External_storage's Document/TestPurpose/ Folder. How it can be done??

推荐答案

    private void downloadFile() {
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl("<your_bucket>");
    StorageReference  islandRef = storageRef.child("file.txt");

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

    final File localFile = new File(rootPath,"imageName.txt");

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

这篇关于如何将文件从Firebase存储下载到Android的External_Storage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆