簡體   English   中英

如何使用PhoneGap在Android SdCard中下載和保存文件(APK)?

[英]How to download and save a file(APK) in android SdCard using PhoneGap?

我正在嘗試從服務器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);
            });
        }
  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();
                                }
                            })
                        });
                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM