Firebase存储下载文件IO异常

时间:2018-02-25 13:54:06

标签: android file firebase firebase-storage

我想将文件下载到我的本地存储,一切正常,直到我添加了另一个类并将它们捆绑在IFormattable接口中。

我想要做的是首先检查我的内存上是否已经有文件,如果没有,请下载它。在任何一种情况下,我都将它绑定到imageview。

public void setDrawable(Context context, IFormattable item, ImageView binding)
    {
        File file = localStorageHelper.getImageFile(item);
        if(file.length() > 0)
        {
            Glide.with(context)
                    .load(file)
                    .into(binding);
        }
        else
        {
            StorageReference fileRef = storageRef.child(item.formatImageFile());
            fileRef.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Glide.with(context)
                            .using(new FirebaseImageLoader())
                            .load(firebaseStorage.getReference(item.formatImageFile()))
                            .into(binding);
                }
            });
        }
    }

问题是我现在突然得到这个错误:

E/FileDownloadTask: Exception occurred during file write.  Aborting.
                    java.io.IOException: No such file or directory
                        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
                        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
                        at java.io.File.createNewFile(File.java:1000)
                        at com.google.firebase.storage.FileDownloadTask.zza(Unknown Source:75)
                        at com.google.firebase.storage.FileDownloadTask.run(Unknown Source:190)
                        at com.google.firebase.storage.zzs.run(Unknown Source:2)
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                        at java.lang.Thread.run(Thread.java:764)
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
E/StorageException: No such file or directory
                    java.io.IOException: No such file or directory
                        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
                        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
                        at java.io.File.createNewFile(File.java:1000)
                        at com.google.firebase.storage.FileDownloadTask.zza(Unknown Source:75)
                        at com.google.firebase.storage.FileDownloadTask.run(Unknown Source:190)
                        at com.google.firebase.storage.zzs.run(Unknown Source:2)
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                        at java.lang.Thread.run(Thread.java:764)
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
E/StorageException: No such file or directory
                    java.io.IOException: No such file or directory
                        at java.io.UnixFileSystem.createFileExclusively0(Native Method)
                        at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
                        at java.io.File.createNewFile(File.java:1000)
                        at com.google.firebase.storage.FileDownloadTask.zza(Unknown Source:75)
                        at com.google.firebase.storage.FileDownloadTask.run(Unknown Source:190)
                        at com.google.firebase.storage.zzs.run(Unknown Source:2)
                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)

我不知道究竟是什么问题,错误描述太模糊了。我确定的是文件确实存在且路径正确。

2 个答案:

答案 0 :(得分:0)

我认为问题是您正在使用API​​级别26+的手机上运行此功能,并且从26+开始,您必须明确要求WRITE_EXTERNAL_STORAGE权限,仅要求READ_EXTERNAL_STORAGE无效。阅读此答案here,了解更多信息。

答案 1 :(得分:0)

我遇到了类似的问题,就我而言,我能够下载某些文件,例如小文本文件,但是大文件带来了例外。

解决方案:

由于.getFile()直接下载到设备,因此我将getFile替换为getDownloadUrl()