从url下载文件到cordova应用程序时出错

5

我正在开发一个需要从我的网站下载图片并将其存储在手机上的应用程序,但当我尝试使用phonegap时,它会显示可能出现的所有错误。我该怎么做才能纠正这个问题 =/ ?


(注:phonegap是一款跨平台移动应用程序开发框架)
var fileTransfer = new FileTransfer();
fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    "/",
    function(entry) {
        alert("download complete: " + entry.fullPath);
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("upload error code" + error.code);
    });

显示的错误信息是:
Download error source " the url used"
download error target: " the target used  "
upload error code 1

我使用的是cordova 2.2.0版本。
以下是logcat错误日志:
 12-06 09:07:26.413: E/FileTransfer(2186): {"target":"\/","source":"http:\/\/developer.android.com\/assets\/images\/home\/ics-android.png","code":1}
12-06 09:07:26.413: E/FileTransfer(2186): java.io.FileNotFoundException
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer.getFileFromPath(FileTransfer.java:794)
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer.access$700(FileTransfer.java:62)
12-06 09:07:26.413: E/FileTransfer(2186):   at org.apache.cordova.FileTransfer$4.run(FileTransfer.java:631)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-06 09:07:26.413: E/FileTransfer(2186):   at java.lang.Thread.run(Thread.java:856)
2个回答

9

您离成功已经很接近了,只是您的目标文件有误。建议尝试以下方式:

var fileTransfer = new FileTransfer();
fileTransfer.download(
    "http://developer.android.com/assets/images/home/ics-android.png",
    "file://sdcard/ics-android.png",
    function(entry) {
        alert("download complete: " + entry.fullPath);
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("upload error code" + error.code);
    });

你需要加上 "file://" 前缀,并且由于没有权限,不能保存到 "/"。

@Simon MacDonald 我也尝试了相同的方式,但出现了错误:ReferenceError: FileTransfer未定义。可能是什么问题呢?请帮帮忙。 - Deepika Lalra
对我没用。我正在genymotion android emulator上测试,我的路径是file://storage/emulated/0/Download/nn.txt。这可能是一个权限问题吗?另外,如何请求在/位置保存的权限? - coding_idiot
更新 我尝试了不同的变化,甚至是与此处发布的相同代码,到目前为止都没有成功。 - coding_idiot
我已经成功让它工作了(在cordova 3.4中),使用下面的答案,谢谢! - coding_idiot
1
对于其他遇到同样问题的人,需要注意的是,在3.4版本中添加“file://”是行不通的。 - coding_idiot

2

我已经尝试过这个方法,在安卓上可以工作,但是我还没有在iOS上检查。下载成功了,因为我正在我的项目中使用它。

enter code here
 function fun(){
var dfd = $.Deferred(function (dfd){
var remoteFile = "Your link";
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localFileName, {create: true, exclusive: false},
    function(fileEntry) {
var localPath = fileEntry.fullPath;
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
            localPath = localPath.substring(7);
            }// You need to write IOS instead of Android

    var ft = new FileTransfer();
    ft.download(remoteFile, localPath, function(entry) {
    dfd.resolve('file downloaded');
     // Do what you want with successful file downloaded and then
    // call the method again to get the next file
    //downloadFile();
            }, fail);
             }, fail);
            }, fail);

           });
            return dfd.promise();

        }
                fun().then(function(msg){
            if(msg==="file downloaded")
            {
            alert("Download complete");
            }
            else
            {
            alert("Download error")
            }
            });
            function fail(){
            alert("error");
        }

我尝试了你的代码,它显示“device未定义”。我删除了device检查,现在它显示错误弹出窗口。 - coding_idiot
根据cordova 3.4中的bug,我们必须使用.toURL()而不是.fullPath() - coding_idiot
1
如果设备未定义,则必须在app/res/xml/plugins.xml中添加权限并添加以下行:<plugin name="Device" value="org.apache.cordova.Device" />,并在app/AndroidManifest.xml中添加以下行:<uses-permission android:name="android.permission.READ_PHONE_STATE" />,然后它将不会显示任何错误,例如设备未定义。 - Anuj Dubey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接