如何将文件从Firebase存储下载到自定义文件夹?

时间:2019-09-30 18:47:29

标签: android firebase firebase-storage

即使在堆栈溢出的情况下,我也整天都在搜索,但是似乎所有代码都无法正常工作。我想将文件从FirebaseStorage下载到内部/外部存储上的自定义文件夹,但是没有任何运气。

我的代码:

private void downloadfile() {

    mStorageRef = FirebaseStorage.getInstance().getReference();
    ref = mStorageRef.child("song.mp3");

    try {
        File localFile = File.createTempFile("song", "mp3");
    } catch (IOException e) {
        e.printStackTrace();
    }

    File storagePath = new File(Environment.getExternalStorageDirectory(), "My Folder");
        // Create direcorty if not exists
    if(!storagePath.exists()) {
        storagePath.mkdirs();
    }

    final File myFile = new File(storagePath,"song.mp3");

    mStorageRef.getFile(myFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            // Local temp file has been created
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

}

1 个答案:

答案 0 :(得分:0)

1。)创建文件路径

String sdcardPath = Environment.getExternalStorageDirectory().toString();
String filePath = sdCardPath + "<YOUR_PATH_LIKE_/Downloads>" + "Filename";

2。)创建文件:

File directory = new File(filePath);