bug: FileSystem.writeFile crash on Android 11
See original GitHub issueBug 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:
- Created 3 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top GitHub Comments
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
Is this still planned for Capacitor 3?