Phonegap下载的图像在存储卡上

Phonegap downloaded image on sdcard

本文关键字:存储卡 图像 下载 Phonegap      更新时间:2023-09-26

我想用phonegap在sd卡(android)上存储一个图像。但这并没有奏效。我用filetransfer.download下载图像。但它会在错误回调中跳转。我得到的错误码是1。这里是我的代码:

window.resolveLocalFileSystemURI(cordova.file.externalRootDirectory  + "/Pictures", function (fileSystem) {
        var fileTransfer = new FileTransfer();
        var uri = encodeURI("MyTopsecretPHPFunctionThatReturnsAnImage...");
        var path = fileSystem.toURL() + "example.jpg";
        alert(path);
        fileTransfer.download(
            uri,
            path,
        function(entry) {
            alert("success!!" + path);
            refreshMedia.refresh(path); // Refresh the image gallery
        },
        function(error) {
            alert(error.source);
            alert(error.target);
            alert(error.code);
        },
        false, 
            { headers: { "Authorization": "dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" }
        });
    },function(error) {
        alert(error);
});

当我把它放在cordova.file.externalDataDirectory时,它正在工作。为什么我不能访问图片文件夹,或者sd卡根目录下的任何其他文件夹?

还有一个问题:在IOS上我可以把图片存储在哪里?我想在画廊里展示它……

我找到了一个解决方案:我只需添加Line:

fileSystem.getDirectory("Pictures", {create: true, exclusive: false});

现在我可以获得存储卡上的访问权限了。