capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

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

Open longinius opened this issue 2 years ago • 9 comments

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)  

longinius avatar Oct 09 '23 13:10 longinius

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.

ionitron-bot[bot] avatar Oct 09 '23 17:10 ionitron-bot[bot]

Same issue here :,)

dusto2020 avatar Dec 29 '23 15:12 dusto2020

any fix on this issue?

barbar-binks avatar Jan 12 '24 18:01 barbar-binks

Same issue here

hvma411 avatar Jan 19 '24 11:01 hvma411

Same here.

distante avatar Jan 31 '24 07:01 distante

And same...

mrtnrs avatar Feb 10 '24 15:02 mrtnrs

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

lokeshpaduchuriiha avatar Apr 22 '24 21:04 lokeshpaduchuriiha