3

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?

Pjottur
  • 632
  • 9
  • 22

2 Answers2

2

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

Community
  • 1
  • 1
Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
1

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.

oshevans
  • 116
  • 1
  • 7