创建一个文件夹并将文件存储在其中

时间:2016-07-13 15:32:38

标签: android

我正在使用firebase下载文件。下面的代码下载文件但是下载到根目录。我想要的是在根目录中创建一个文件夹并将下载的文件存储在其中。

FirebaseStorage storage = FirebaseStorage.getInstance();
                    StorageReference storageRef = storage.getReferenceFromUrl("gs://fir-896a6.appspot.com");

                    StorageReference islandRef = storageRef.child(fileName);
                    File localFile = null;
                    try {
                        localFile = new File(Environment.getExternalStorageDirectory(), fileName);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    islandRef.getFile(localFile);

我尝试过使用

localFile = new File(Environment.getExternalStorageDirectory() + "/Student Portal/", fileName);
                        localFile.mkdirs();

它创建文件夹Student Portal但不是下载文件并将其保存在文件夹中,而是在名为fileName的Student Portal文件夹中创建文件夹并抛出错误

    E/FileDownloadTask: Exception occurred during file download
                                                                                    java.io.FileNotFoundException: /storage/emulated/0/Student Portal/proposal-1.docx: open failed: EISDIR (Is a directory)
                                                                                        at libcore.io.IoBridge.open(IoBridge.java:456)
                                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
                                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
                                                                                        at com.google.firebase.storage.FileDownloadTask.run(Unknown Source)
                                                                                        at com.google.firebase.storage.StorageTask$5.run(Unknown Source)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                        at java.lang.Thread.run(Thread.java:818)
                                                                                     Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
                                                                                        at libcore.io.Posix.open(Native Method)
                                                                                        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
                                                                                        at libcore.io.IoBridge.open(IoBridge.java:442)
                                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
                                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:72) 
                                                                                        at com.google.firebase.storage.FileDownloadTask.run(Unknown Source) 
                                                                                        at com.google.firebase.storage.StorageTask$5.run(Unknown Source) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                        at java.lang.Thread.run(Thread.java:818) 
07-13 21:14:39.217 14894-15750/com.horngmail.invisible.firebase E/StorageException: StorageException has occurred.
                                                                                    An unknown error occurred, please check the HTTP result code and inner exception for server response.
                                                                                     Code: -13000 HttpResult: 200
07-13 21:14:39.217 14894-15750/com.horngmail.invisible.firebase E/StorageException: /storage/emulated/0/Student Portal/proposal-1.docx: open failed: EISDIR (Is a directory)
                                                                                    java.io.FileNotFoundException: /storage/emulated/0/Student Portal/proposal-1.docx: open failed: EISDIR (Is a directory)
                                                                                        at libcore.io.IoBridge.open(IoBridge.java:456)
                                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
                                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
                                                                                        at com.google.firebase.storage.FileDownloadTask.run(Unknown Source)
                                                                                        at com.google.firebase.storage.StorageTask$5.run(Unknown Source)


                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                    at java.lang.Thread.run(Thread.java:818)


                                  Caused by: android.system.ErrnoException: open failed: EISDIR (Is a directory)
                                                                                at libcore.io.Posix.open(Native Method)
                                                                                at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
                                                                                at libcore.io.IoBridge.open(IoBridge.java:442)

1 个答案:

答案 0 :(得分:1)

 localFile.mkdirs();

更改为

 localFile.getParentFile().mkdirs();

首先使用fileName删除该目录。