EugeneVB
EugeneVB

Reputation: 5

Write file to SD card root directory with ionic/cordova app

I need to write file to root directory of sd-card. I use getExternalSdCardDetails() function from cordova-diagnostic-plugin. And get something like this:

[{
     "path": "/storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files",
     "filePath": "file:///storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files",
     "canWrite": true,
     "freeSpace": 16254009344,
     "type": "application"
 }, {
     "path": "/storage/4975-1401",
     "filePath": "file:///storage/4975-1401",
     "canWrite": false,
     "freeSpace": 16254009344,
      "type": "root"
}]

So, I can't write to file:///storage/4975-1401, because "canWrite": false I've add

<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" /> 

to config.xml and

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

to AndroidManifest.xml, but no results, when I try write file to root directory, I get error CODE 5 ENCODING_ERR

Writing to /storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files is working.

Upvotes: 1

Views: 694

Answers (1)

DaveAlden
DaveAlden

Reputation: 30356

Since Android 4.4 Kitkat, non-system apps have read-only permission to secondary storage volumes (i.e. removable SD cards) outside of their application sandbox area (/Android/data/[package-id]/). See here for a more detailed explanation.

So the getExternalSdCardDetails() function is correctly informing you that you can write to the application sandbox directory /storage/4975-1401/Android/data/cordova.plugins.diagnostic.example/files but not to the root directory /storage/4975-1401/.

There is no way around this on Android 4.4 and above unless the device is rooted.

Upvotes: 1

Related Questions