In a cordova android app (scroll to the bottom for device,version, plugin list, all of it quite up-to-date as of this writing) I want to preferentially store file on the external SDCard and use internal storage if SDCard not there. I am setting my save path to:
persistentFS= cordova.file.externalDataDirectory||cordova.file.DataDirectory||fileSystem.root.toURL();
Its eventual value is file:///storage/emulated/0/Android/data/com.fubar.app/files/, same as cordova.file.externalDataDirectory.
I have set:
<access origin="cdvfile://*" />
...
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="files-external,sdcard,files,documents,cache,cache-external,root" />
in config.xml,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in AndroidManifest.xml and set various enchantments in index.html Security metatag. I add that a SDcard is inserted, write-enabled and I confirm that I can write to it with - for example - a file manger. It contains the Android/data/com.fubar.myapp/files/ folder, as it should.
All for nothing: files get written to - and read from - the internal device storage. I have read all google scavenged on this topic but nothing has brought me closer to the task (some folks suggest using the file-system-roots plugin, but it appears to have been subsumed by the file plugin).
Any hints? TIA, alf
Edit: Using adb shell, I can see that the external sdcard files folder for the app has an absolute path of:
`/storage/extSdCard/Android/data/com.fubar.myapp/files`
and calling resolveLocalFileSystemURL on
file:///storage/extSdCard/Android/data/com.fubar.myapp/files
happily succeeds. I do not think that is the way to go, as that path is device dependent.
Device: Samsung Galaxy tab 10.5, lollipop 5.0.1 using cordova 5.3.1, with plugins: cordova-plugin-device 1.0.1 "Device" cordova-plugin-dialogs 1.1.1 "Notification" cordova-plugin-file 3.0.0 "File" cordova-plugin-file-transfer 1.3.0 "File Transfer" cordova-plugin-media 1.0.1 "Media" cordova-plugin-whitelist 1.1.0 "Whitelist"
Build host is Linux FC21
You can call Context.getExternalFilesDirs()
. That returns an array of directories to which your app can write data. One of which will be the sdcard path /storage/sdcardname/Android/data/appname/files.
Most manufacturers also set the SECONDARY_STORAGE environment variable to expose the SD Card root path. You can try calling System.getenv("SECONDARY_STORAGE")
Android does not expose any API that allows you to get the path of the sdcard root path. You can use reflection and obtain the sdcard root path from StorageManager.java -> getVolumePaths() - http://androidxref.com/5.1.1_r6/xref/frameworks/base/core/java/android/os/storage/StorageManager.java#590
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With