【问题标题】:How to download and save a file(APK) in android SdCard using PhoneGap?如何使用 PhoneGap 在 android SdCard 中下载和保存文件(APK)?
【发布时间】:2016-04-14 16:33:11
【问题描述】:

我正在尝试从服务器 url 下载并保存 apk。但我无法下载并保存。你能帮我解决这个问题吗?

代码:

onDeviceReady: function() {

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
app.receivedEvent('deviceready');
myApp.start(); //this is where I put the call to my App's functionality relying on device APIs
}


    function gotFS(fileSystem) {
            window.fileSystem = fileSystem;
            fileSystem.root.getDirectory(window.appRootDirName, {
                create: true,
                exclusive: false
            }, dirReady, fail);
        }

        function dirReady(entry) {
            window.appRootDir = entry;
            console.log("application dir is ready");
           // alert("ready");
             var fileTransfer = new FileTransfer();

            var url = "http://www.something.org/Android/Sample.apk";

            var filePath = window.appRootDir.toURL() + "Sample.apk";
                            alert(filePath);

             var uri = encodeURI(url.android);
            fileTransfer.download(
            uri, filePath, function(entry) {
                alert("download complete: " + entry.fullPath);

        window.plugins.webintent.startActivity({
        action: window.plugins.webintent.ACTION_VIEW,
        url: 'file://' + entry.fullPath,
        type: 'application/vnd.android.package-archive'
        },
        function(){
            console.log("Success");
        },
        function(e){
            alert('Error launching app update');
        }
    );          


            }, function(error) {
                alert("download error" + error.codename);
            });
        }

【问题讨论】:

    标签: javascript android html cordova


    【解决方案1】:
      var url = "http://www.something.org/Android/Sample.apk";
                            var targetPath = "file:///storage/sdcard0/Download/aplstar" + APPVER + ".apk"; 
                            var trustHosts = true
                            var options = {};
                            $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) {
    
                                $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive'
                                ).then(function () {
                                    $ionicLoading.show({
                                        template: "Please select appropriate option to update"
                                    });
                                                                    }, function (err) {
                                    //
                                    $ionicLoading.show({
                                        template: "Unable to update."
                                    });
                                });
                                $ionicLoading.hide();
                            }, function (err) {
                                alert('failed download progress');
                            }, function (progress) {
                                $timeout(function () {
                                    var downloadProgress = (progress.loaded / progress.total) * 100;
                                    if (downloadProgress) {
                                        var progressBar = document.getElementById('progressbar').children[0];
                                        var count = document.getElementById('count');
                                        count.innerHtml = parseInt(downloadProgress).toString() + '%';
                                        progressBar.style.width = downloadProgress + '%';
                                    }
    
                                    if (downloadProgress > 99) {
                                        $ionicLoading.hide();
                                    }
                                })
                            });
                        }
    

    【讨论】:

    • 您能否也对您提出的代码提供一些解释。只需一个代码 sn-p 就可以完成这项工作,但并没有它应有的教育意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2012-04-16
    • 1970-01-01
    相关资源
    最近更新 更多