Pjottur
Pjottur

Reputation: 642

Phonegap/Cordova cannot download to subfolder in Android

I'm building an Android app in Eclipse with Phonegap 2.2.0

This worked in iOS:

var uri = encodeURI(value);
var fileName = uri.substring(uri.lastIndexOf('/')+1);

 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
        fileSystem.root.getDirectory("dir/subdir", {create: true, exclusive: false}, function(dirEntry){
            dirEntry.getFile(fileName, {create: true, exclusive: false}, function(fileEntry) {
                var localPath = fileEntry.fullPath;
                var fileTransfer = new FileTransfer();
                fileTransfer.download(
                    uri,
                    localPath,
                    function(entry) {
                        console.log("xfg download complete: " + entry.fullPath);
                    },
                    function(error) {
                        console.log("xfg download error source " + error.source);
                        console.log("xfg download error target " + error.target);
                        console.log("xfg upload error code" + error.code);
                    }
                );

            });
        });
    });

On line 4 of the above code, I am getting the directory at "dir/subdir" and the download works fine. In Android, however, the fileSystem gets the subdirectory, but the download fails with "file not found".

If I replace "dir/subdir" with "dir" it works.

Any solutions or clever workarounds to this?

Upvotes: 3

Views: 892

Answers (2)

oshevans
oshevans

Reputation: 116

You can't specify a subdirectory unless the directory already exists. So create getDirectory('dir'... then getDirectory('subdir'...

https://developer.mozilla.org/en-US/docs/DOM/File_API/File_System_API/DirectoryEntry :

Either an absolute path or a relative path from the DirectoryEntry to the directory to be looked up or created. It is an error to attempt to create a file whose immediate parent does not yet exist.

Upvotes: 1

Gunnar Karlsson
Gunnar Karlsson

Reputation: 28480

You can identify the device type by probing the navigator object's userAgent property:

if((navigator.userAgent.match(/Android/i)) == "Android")

and if it is an Android device, use dir instead of dir/subdir.

See: Detect device type in phonegap

Upvotes: 2

Related Questions