【问题标题】:Cordova file plugin not working when I try to download img on android当我尝试在 android 上下载 img 时,Cordova 文件插件不起作用
【发布时间】:2022-10-13 13:29:35
【问题描述】:

我使用带有文件插件的cordova。 我尝试了来自不同论坛的许多解决方案,但似乎都没有奏效。

<!DOCTYPE html>
<html>
    <head>
        <title>aaa</title>
        <script src="js.js"></script>
    </head>
    <body>
            <span onclick="testDownload()">click</span>
    </body>
</html>

这是来自:https://stackoverflow.com/a/53083810/16003367

function DownloadToDevice(fileurl) {
  var blob = null;
  var xhr = new XMLHttpRequest();
  xhr.open("GET", fileurl);
  xhr.responseType = "blob";//force the HTTP response, response-type header to be blob
  xhr.onload = function()
  {
      blob = xhr.response;//xhr.response is now a blob object
      console.log(blob);
      var storageLocation = "";
//     switch (device.platform) {
//         case "Android":
             storageLocation = 'file:///storage/emulated/0/';
//             break;
//         case "iOS":
//             storageLocation = cordova.file.documentsDirectory;
//             break;
//     }
     var folderpath = storageLocation + "Download";
     var filename = "Myimg.png";
     var DataBlob = blob;
      window.resolveLocalFileSystemURL(folderpath, function(dir) {
        dir.getFile(filename, {create:true}, function(file) {
                file.createWriter(function(fileWriter) {
                    fileWriter.write(DataBlob);
                    //Download was succesfull
                }, function(err){
                  // failed
                  console.log(err);
                });
        });
      });
  };
  xhr.send();
}


function testDownload() {
    DownloadToDevice('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSSPkOcqTJH4tkT_Zlit1poKuS9TPWNylC7qg&usqp=CAU');
}

我的配置文件:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.hello.aaaa" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>aaa</name>
    <description>Sample Apache Cordova App</description>
    <author email="dev@cordova.apache.org" href="https://cordova.apache.org">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <preference name="AndroidPersistentFileLocation" value="Compatibility" />
    <preference name="AndroidExtraFilesystems" value="files,cache, sdcard, cache-external, files-external" />

    <edit-config file="AndroidManifest.xml" target="/manifest/uses-sdk" mode="merge">
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    </edit-config>
</widget>

我正在 android studio 模拟器中进行测试。 这不是网络问题,因为从浏览器下载随机文件有效。 我在设置 -> 应用程序设置 -> myapp -> 应用程序权限中授予了权限

当我按下按钮时没有任何反应。我错过了什么?请帮忙

【问题讨论】:

    标签: javascript android file cordova


    【解决方案1】:

    我用它来下载图像:

    function imageExists(image_url){
    
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
    
        //console.log('file system open: ' + fs.name);
        getSampleFile(fs.root, image_url);
    
    }, onErrorRequestFS);
    
    return true;
    

    }

    函数 getSampleFile(dirEntry, image_url) {

    //window.resolveLocalFileSystemURL("file:///android_asset/www/img/" + image_url, fileExists, 
    //window.resolveLocalFileSystemURL(cordova.file.dataDirectory + image_url, fileExists, 
    window.resolveLocalFileSystemURL(dirEntry.nativeURL + image_url, fileExists, 
    
        function () { 
        
            var xhr = new XMLHttpRequest();
            xhr.open('GET', "https://www.mondomain.com/" + image_url, true);
            xhr.responseType = 'blob';
    
            xhr.onload = function() {
                if (this.status == 200) {
                    //console.log("file exists on server : " + JSON.stringify(dirEntry) + image_url);
                    var blob = new Blob([this.response], { type: 'image/png' });
                    
                    saveFile(dirEntry, blob, image_url);
                }
                else {
                    console.log("file not exists on server : " + this.status);
                }
            };
            xhr.send();
        }   
    );
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 2016-09-10
      相关资源
      最近更新 更多