Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@capacitor/filesystem downloadFile() fails to create missing directories under android #1835

Open
longinius opened this issue Oct 9, 2023 · 11 comments

Comments

@longinius
Copy link

Bug Report

The downloadFile() method doesn't create missing directories under android.

Plugin(s)

@capacitor/[email protected]

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 5.4.2
  @capacitor/core: 5.4.2
  @capacitor/android: 5.4.2
  @capacitor/ios: 5.4.2

Installed Dependencies:

  @capacitor/core: 5.4.2
  @capacitor/android: 5.4.2
  @capacitor/cli: 5.4.2
  @capacitor/ios: 5.4.2

[success] iOS looking great! 👌
[success] Android looking great! 👌

Platform(s)

Android

Current Behavior

Download fails on android, when setting the option recursive to true and providing a path with subdirectories that doesn't exist.

Filesystem.downloadFile({
  url: 'https://images.unsplash.com/photo-1510158105534-9b01f2462ce1',
  path: '/path/to/file/image.jpg',
  directory: Directory.Cache,
  recursive: true,
});

The above code will result in the following error.

ERROR Error: Uncaught (in promise): Error: Error downloading file: /data/user/0/io.ionic.starter/cache/path/to/file/image.jpg: open failed: ENOENT (No such file or directory)
Error: Error downloading file: /data/user/0/io.ionic.starter/cache/path/to/file/image.jpg: open failed: ENOENT (No such file or directory)

Expected Behavior

The missing folders should be created and the file should be saved at the specified path. On iOS, the missing folders are created.

Code Reproduction

Repository with minimal reproduction

Other Technical Details

Tested on native android device with android version 13.

Changing or omitting the directory option does not change the behavior, only the error message becomes slightly different. If you create the folder beforehand with the mkdir function, the download also works.

Additional Context

Error in Logcat
Error downloading file: /data/user/0/io.ionic.starter/cache/path/to/file/image.jpg: open failed: ENOENT (No such file or directory)
    java.io.FileNotFoundException: /data/user/0/io.ionic.starter/cache/path/to/file/image.jpg: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:574)
    at java.io.FileOutputStream.(FileOutputStream.java:236)
    at com.capacitorjs.plugins.filesystem.Filesystem.downloadFile(Filesystem.java:339)
    at com.capacitorjs.plugins.filesystem.FilesystemPlugin.downloadFile(FilesystemPlugin.java:401)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138)
    at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:780)
    at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0)
    at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Linux.open(Native Method)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:274)
    at libcore.io.ForwardingOs.open(ForwardingOs.java:563)
    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7804)
    at libcore.io.IoBridge.open(IoBridge.java:560)
    at java.io.FileOutputStream.(FileOutputStream.java:236) 
    at com.capacitorjs.plugins.filesystem.Filesystem.downloadFile(Filesystem.java:339) 
    at com.capacitorjs.plugins.filesystem.FilesystemPlugin.downloadFile(FilesystemPlugin.java:401) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:138) 
    at com.getcapacitor.Bridge.lambda$callPluginMethod$0(Bridge.java:780) 
    at com.getcapacitor.Bridge.$r8$lambda$ehFTi5f4HhVNFKTbCKAYDkpQYRA(Unknown Source:0) 
    at com.getcapacitor.Bridge$$ExternalSyntheticLambda3.run(Unknown Source:8) 
    at android.os.Handler.handleCallback(Handler.java:942) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loopOnce(Looper.java:201) 
    at android.os.Looper.loop(Looper.java:288) 
    at android.os.HandlerThread.run(HandlerThread.java:67)  
@ionitron-bot
Copy link

ionitron-bot bot commented Oct 9, 2023

This issue has been labeled as type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.

@dusto2020
Copy link

Same issue here :,)

@barbar-binks
Copy link

any fix on this issue?

@hvma411
Copy link

hvma411 commented Jan 19, 2024

Same issue here

@distante
Copy link

Same here.

@mrtnrs
Copy link

mrtnrs commented Feb 10, 2024

And same...

@lokeshpaduchuriiha
Copy link

This issue is still persisting. Do we know the solution for this issue. Also, the getUri() fails and crashes the app.

@aalzehla
Copy link

Any solution

@webfletch
Copy link

Still not fixed in version 6.0.1

@Brullworfel
Copy link

Still not fixed in 6.0.2.

@Brullworfel
Copy link

Brullworfel commented Dec 15, 2024

Temporary workaround, before downloading:

if(Capacitor.getPlatform() === 'android'){
  const dirPath = filePath.split('/').slice(0,-1).join('/');
  await Filesystem.mkdir({
     path: dirPath,
     directory: Directory.Data,
     recursive: true,
   }).catch(()=>null); //ignore error if directory is already there
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests