我想将文件下载到本地存储,一切正常,直到我添加另一个类并将它们捆绑在 IFormattable 接口中。
我想做的是首先检查我的内存中是否已经有该文件,如果没有,请下载它。无论哪种情况,我都会将其绑定到图像视图。
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)
我不知道到底是什么问题,错误描述太模糊了。我确信该文件确实存在并且路径是正确的。
我认为问题是你在 API 级别 26+ 的手机上运行这个程序,从 26+ 开始你必须明确请求 WRITE_EXTERNAL_STORAGE 权限,只请求 READ_EXTERNAL_STORAGE 是行不通的。请阅读此答案此处以获取更多信息。
我面临着类似的问题,就我而言,我能够下载某些文件,例如小文本文件,但大文件带来了异常。
解决方案:
我将
.getFile()
替换为 getDownloadUrl(),因为 getFile
直接下载到设备
对于我来说,我首先要创建文件,似乎如果之前没有创建目录,就会出错。 所以在 FLutter 中,我必须这样做:
Directory appDocDir = await getApplicationDocumentsDirectory();
File downloadToFile = File(
'${appDocDir.path}/myPath/myFile.txt');
await downloadToFile.create(recursive: true);
await FirebaseStorage.instance
.ref("downloadPath")
.writeToFile(downloadToFile);
这是 recursive 参数为我创建目录的东西,这确实为我解决了问题。
我知道flutter代码不是OP所要求的,但它是为了演示,以便任何人都可以将其转换为他需要的语言。