如何将文件从Firebase存储下载到Android的External_Storage

Sid*_*Sid 2 android firebase firebase-storage

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

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

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


        }


    }
Run Code Online (Sandbox Code Playgroud)

我想将Firstfile.pdf文件下载到External_storage的Document/TestPurpose/Folder.怎么做?

Ste*_*uvr 14

    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());
        }
    });
}
Run Code Online (Sandbox Code Playgroud)