Andi Andi - 1 month ago 43
Android Question

How to access external storage with cordova file / file-system-roots plugins?

Problem description:
I can access the internal storage with either the file or the file-system-roots (read and write). But such a file can not be access from an other app. For example if i want to send this file through the emailComposerPlugin, the file can not be accessed by the email client. (Same goes for the "open with" function.)
If i change the options

{sandboxed: true}
to false (to write to the external storage), it does not work and ends up in a FileUtils.UNKNOWN_ERR. I tried the application while the phone disconnected from USB, as some docu mention that external storage can not be accessed while mounted on the pc - same result though.

From what i read on the mailing list this should be possible. It seams i miss a crucial point?

I try to enable an hybrid application created for iPhone to run on android devices. To have a little playground, i create a small test project.

There seams to be a problem between file-system-roots and file plugin. But i have the newest versions of both of them. (File: 1.0.1 File-system-roots: 0.1.0)
Debugging the file-system and file classes show that

private String fullPathForLocalURL(Uri URL) {
if (FILESYSTEM_PROTOCOL.equals(URL.getScheme()) && "localhost".equals(URL.getHost())) {
String path = URL.getPath();
if (URL.getQuery() != null) {
path = path + "?" + URL.getQuery();
return path.substring(path.indexOf('/', 1));
// path = "/cache-external" at this point
// results in index out of bounds exception

What have i tried?


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


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

javascript code

function createTextDocument(filename, text) {

cordova.filesystem.getDirectoryForPurpose('cache', {sandboxed: false}, successCallback, failureCallback);

function successCallback(directoryEntry){
console.log('directory found (cordova): ' + directoryEntry.toURL());
console.log('directory found (native) : ' + directoryEntry.toNativeURL());
directoryEntry.getFile(filename, {create: true, exclusive: false},
var filePath = fileEntry.toNativeURL();
console.log('start writing to: ' + filePath );
console.log('file written');
}, failureCallback

function failureCallback(error){
console.log('error creating file: ' + error.code);
// results in code 1000


After digging this whole topic quite a bit, i figured out:

  • There is no need for the file system roots plugin.
  • There is more configuration needed in config.xml.
  • You need to use not the standard FileApi way, but the following to access the files.

JavaScript usage:

window.resolveLocalFileSystemURL(path, cbSuccess, cbFail);

@param path: {string} a cordova path with scheme: 
             Examples: 'cdvfile://localhost/sdcard/path/to/global/file'
@param cbSuccess: {function} a callback method that receives a DirectoryEntry object.
@param cbSuccess: {Function} a callback method that receives a FileError object.


<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />


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