cordova 文件插件不工作时,我尝试下载的Android上的img

vqlkdk9b  于 2022-11-15  发布在  Android
关注(0)|答案(1)|浏览(144)

我使用 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 -〉应用程序权限中授予了权限
第一次
当我按下按钮时什么也没发生。我错过了什么?请帮帮忙

3yhwsihp

3yhwsihp1#

我用这个下载图片:

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(目录条目,图像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();
    }   
);

}
函数saveFile(目录条目,文件数据,文件名){

dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
    
    writeFile(fileEntry, fileData);
    
}, onErrorSaveFileFS);

}
函数writeFile(文件条目,数据对象,isAppend){
文件条目。创建写入器(函数(文件写入器){

fileWriter.onwriteend = function() {
        //console.log("Successful file write...");
        /*if (dataObj.type == "image/png") {
            readBinaryFile(fileEntry);
        }
        else {
            readFile(fileEntry);
        }*/
    };

    fileWriter.onerror = function(e) {
        console.log("Failed file write: " + e.toString());
    };

    fileWriter.write(dataObj);
});

}

function onErrorRequestFS(err) {
    console.log("File onErrorRequestFS" + err.code);
}

function onErrorSaveFileFS(err) {
    console.log("File onErrorSaveFileFS" + err.code);
}

相关问题