@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.
I think this can be helpful.
I've found out that starting from Android 11, the system ignores the requestLegacyExternalStorage
attribute.
More information here.
Since I'm developing an app for research and my phone has Android 11, so I cannot target any lower version, I ask if there is any other way I can write files locations that are not part of what is considered legacy storage.
@AlessandroFBK We've been keeping an eye on this issue for Capacitor 3, which we've said will support Android 11. See ionic-team/capacitor-plugins#169
Right now there's a scary prompt at the top of this page: https://support.google.com/googleplay/android-developer/answer/9956427?hl=en We're waiting to see what will play out, so subscribe to the issue I linked.
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
feat(android): update the filesystem plugin to accommodate scoped storage and permission changes since Android 10
ionic-team/capacitor-plugins#169
This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.
Please see the Contributing Guide for how to create a Code Reproduction.
Thanks!
Ionitron 💙
It looks like this issue didn't get the information it needed, so I'll close it for now. If I made a mistake, sorry! I am just a bot.
Have a great day!
Ionitron 💙