1

I want to download a file to my local storage, everything was working untill I added another class and bundled both together in an IFormattable interface.

What I want to do is to first check wether I allready have the file on my memory and if not, download it. In either case I bind it to an 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);
                }
            });
        }
    }

The problem is that I now all of the sudden get this error:

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)

I have no idea what the problem exactly is, the error description is too vague. What I am certain of is that the file does exist and that the path is correct.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Wouter Vandenputte
  • 1,948
  • 4
  • 26
  • 50
  • 1
    The most likely cause is that the directory that you try to create the file in doesn't exist. See [this question](https://stackoverflow.com/questions/7469018/cannot-make-file-java-io-ioexception-no-such-file-or-directory) or some of the other [pages about this exact stack trace](https://www.google.com/search?q=java.io.File.createNewFile+java.io.IOException%3A+No+such+file+or+directory). – Frank van Puffelen Feb 25 '18 at 15:44

3 Answers3

1

The problem i think is you are running this on a phone with API Level 26+ and from 26+ onwards you have to ask for the WRITE_EXTERNAL_STORAGE permission explicitly, only asking for READ_EXTERNAL_STORAGE wont work. Read this answer here for more info.

shashank chandak
  • 544
  • 5
  • 13
0

I was facing a similar issue, in my case I was able to download certain files such as small text files, but large files brought the exception.

Solution:

I replaced .getFile() to getDownloadUrl() since getFile downloads directly to the device

Oush
  • 3,090
  • 23
  • 22
0

For me I had first to create the file, it seems that if there is no directory made before, there will be an error. So in FLutter, I had to do this:

Directory appDocDir = await getApplicationDocumentsDirectory();
File downloadToFile = File(
              '${appDocDir.path}/myPath/myFile.txt');
await downloadToFile.create(recursive: true);
await FirebaseStorage.instance
                .ref("downloadPath")
                .writeToFile(downloadToFile);

Here is the thing that recursive parameters created the directory for me, that is the thing that did solve the problem for me.

I know that the flutter code is not what the OP is asking for but it's here for demonstration so that anyone can convert it to his needed language.

Shalabyer
  • 555
  • 7
  • 23