question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: FileSystem.writeFile crash on Android 11

See original GitHub issue

Bug Report

Thank you all for your great work! I hope you can help me with this problem.

Capacitor Version

  @capacitor/ios not installed
  @capacitor/cli 2.4.6
  @capacitor/android 2.4.6
  @capacitor/core 2.4.6
  @capacitor/electron not installed

Platform(s)

Android

Current Behavior

The bug occurs when I try to write a new file in the Documents folder using the the Filesystem API. In the log an error is thrown complaining the open operation is not permitted.

2021-01-26 08:26:51.896 3598-3598/io.ionic.starter E/Capacitor/Filesystem: Creating text file '/storage/emulated/0/Documents/experiments/26012021/log_08:26:49.csv' with charset 'UTF-8' failed. Error: /storage/emulated/0/Documents/experiments/26012021/log_08:26:49.csv: open failed: EPERM (Operation not permitted)
    java.io.FileNotFoundException: /storage/emulated/0/Documents/experiments/26012021/log_08:26:49.csv: open failed: EPERM (Operation not permitted)
        at libcore.io.IoBridge.open(IoBridge.java:492)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
        at com.getcapacitor.plugin.Filesystem.saveFile(Filesystem.java:269)
        at com.getcapacitor.plugin.Filesystem.writeFile(Filesystem.java:225)
        at com.getcapacitor.plugin.Filesystem.handleRequestPermissionsResult(Filesystem.java:669)
        at com.getcapacitor.Bridge.onRequestPermissionsResult(Bridge.java:763)
        at com.getcapacitor.BridgeActivity.onRequestPermissionsResult(BridgeActivity.java:206)
        at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:8466)
        at android.app.Activity.dispatchActivityResult(Activity.java:8314)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.system.ErrnoException: open failed: EPERM (Operation not permitted)
        at libcore.io.Linux.open(Native Method)
        at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
        at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
        at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)

I’ve added android:requestLegacyExternalStorage="true" as stated in the documentation which nonetheless mentions only Android 10.

Expected Behavior

The expected behavior should be like on the previous Android version (10). I’ve tested the same code on the emulator and everything works fine.

Code Reproduction

I followed the official doc, so the snipped of code that causes the issue is very similar to one of the examples provided.

  async fileWrite(type: LogEventType, path: string, timestamp: string) {
    await Filesystem.writeFile({
      path: `${FILE_SYSTEM_CONFIGURATION.parentDir}/${path}.csv`,
      data: `${this.logEntryIndex}, ${timestamp}, ${type}\n`,
      directory: FilesystemDirectory.Documents,
      encoding: FilesystemEncoding.UTF8,
      recursive: true
    });

Other Technical Details

npm --version output: 7.4.3

node --version output: v14.8.0

Additional Context

I browsed and found related problems in other issues (link), but none of them targeting Android 11.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
xparadoxoncommented, May 31, 2021

For me it worked to set targetSdkVersion to 29 as in this version requestLegacyExternalStorage is supported, which then works on android 11 devices. Might not be the best way for future, but works. https://developer.android.com/about/versions/11/privacy/storage

2reactions
asztalcommented, Apr 15, 2021

Is this still planned for Capacitor 3? image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Capacitor Filesystem returns error while using main functions
When using Android 11 to create a directory using the mkdir() function I get the following error: Unable to create directory, unknow reason....
Read more >
Capacitor Filesystem error when I try to read a file in Android 11
I tried to read a file in my emulator using the function readFile of capacitor filesystem. The file that I trying manipulate it's...
Read more >
Capacitor 3.3.2 - File permission issues crashing app : r/ionic
I am trying to use file storage to read in files from the camera or photo library. Upon applying the selected camera photo...
Read more >
Data and file storage overview - Android Developers
This purpose-based storage model improves user privacy because apps are given access only to the areas of the device's file system that they...
Read more >
Android 11 App Crashes On Existing Vault Open
Attempt to open existing vault · Let the indexing rocoess start · A few seconds later the UI will get laggy. · The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found