phonegap图片下载错误代码3

phonegap image download error code 3


我正在尝试使用 phonegap 文件传输插件下载方法从 url 下载图像
但总是得到错误代码:3,
这是我的代码:

window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fs)
{
    // create the download directory is doesn't exist
    fs.root.getDirectory('download', { create: true });

    //  save file in .. download
    var filePath = fs.root.fullPath + 'download/' + url.split('/').pop();
    var fileTransfer = new FileTransfer();
    var uri = encodeURI(decodeURIComponent("http://phonegap.com/uploads/artwork/Build-Bot-Preview.png"));
    filePath="file:///sdcard"+filePath;


    fileTransfer.download(uri,filePath,function(entry)
    {
        alert("Successfully downloaded file, full path is " + entry.fullPath);
    },
    function(error)
    {
        alert("Some error " + error.code + " for " + url);
    }, 
    false);
});

谢谢。

我在新创建的 phonegap 项目中尝试了您的代码,它对我有用。我收到 an alert 表示成功。

index.js:

onDeviceReady: function() {
    app.receivedEvent('deviceready');

    var url = "http://animalia-life.com/image.php?pic=/data_images/dog/dog1.jpg";

    window.requestFileSystem(window.LocalFileSystem.PERSISTENT, 0, function(fs)
    {
        // create the download directory is doesn't exist
        fs.root.getDirectory('download', { create: true });

        //  save file in .. download
        var filePath = fs.root.fullPath + 'download/' + url.split('/').pop();
        var fileTransfer = new FileTransfer();
        var uri = encodeURI(decodeURIComponent("http://phonegap.com/uploads/artwork/Build-Bot-Preview.png"));
        filePath="file:///sdcard"+filePath;

        fileTransfer.download(uri,filePath,function(entry)
        {
            alert("Successfully downloaded file, full path is " + entry.fullPath);
        },
        function(error)
        {
            alert("Some error " + error.code + " for " + url);
        }, 
        false);
    });

},

希望对您有所帮助。至少你知道代码现在可以工作了。

编辑:

您的 config.xml 访问源行中是否有:

<access origin="*" />

编辑:编辑:

我在 config.xml:

中用这些行声明插件
<gap:plugin name="cordova-plugin-file" source="npm" version="3.0.0" />
<gap:plugin name="cordova-plugin-file-transfer" source="npm" version="1.3.0" />