2017-02-20 106 views
1

我创建了一个简单的科尔多瓦android应用程序,我试图从网址下载图像到图片库,但我真的不知道什么是错的。 我已经在计算器这里搜索了很多,主要包括以下环节:科尔多瓦 - 从URL下载图像到图片库

Phonegap - Save image from url into device photo gallery

How to save an Image object into a file in Android with Phonegap?

我已经安装了科尔多瓦文件传输的插件,并试图从官方网站做例子,但没有工作过:https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/

我试过2个不同的代码,它们是:

1)第一次尝试:

document.getElementById("myBtn").addEventListener("click", function() { 
    download("http://cordova.apache.org/static/img/cordova_bot.png", "data", "new_file"); 
}); 

function download(URL, Folder_Name, File_Name) { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail); 

    function fileSystemSuccess(fileSystem) { 
     var download_link = encodeURI(URL); 
     ext = download_link.substr(download_link.lastIndexOf('.') + 1); //Get extension of URL 

     var directoryEntry = fileSystem.root; // to get root path of directory 
     directoryEntry.getDirectory(Folder_Name, { 
      create: true, 
      exclusive: false 
     }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard 
     var rootdir = fileSystem.root; 
     var fp = rootdir.toURL(); 
     fp = fp + "/" + Folder_Name + "/" + File_Name + "." + ext; // fullpath and name of the file which we want to give 
     filetransfer(download_link, fp); 
    } 

    function onDirectorySuccess(parent) { 
     // Directory created successfuly 
    } 

    function onDirectoryFail(error) { 
     alert("Unable to create new directory: " + error.code); 
    } 

    function fileSystemFail(evt) { 
     //Unable to access file system 
     alert(evt.target.error.code); 
    } 
} 

function filetransfer(download_link, fp) { 
    var fileTransfer = new FileTransfer(); 
    fileTransfer.download(download_link, fp, 
     function (entry) { 
      alert("download complete: " + entry.fullPath); 
      //cordova.plugins.imagesaver.saveImageToGallery(entry.fullPath, successCallback, errorCallback); 
     }, 
     function (error) { 
      alert("download error source " + error.source); 
     } 
    ); 
} 

在这种尝试中,我得到的警告信息“下载完成:/my_folder/new_file.png”,但我找不到在哪里的图片被下载。 这绝对不是在图片库或任何我能找到它的地方。

2)第二次尝试:

function download() { 
    window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) { 

     var url = 'http://cordova.apache.org/static/img/cordova_bot.png'; 
     fs.root.getFile('downloaded-image.png', { 
      create: true, 
      exclusive: false 
     }, function (fileEntry) { 
      file_transfer(fileEntry, encodeURI(url), true); 

     }, onErrorCreateFile); 

    }, onErrorLoadFs); 
} 

function onErrorLoadFs(msg){ 
    alert(msg); 
} 

function onErrorCreateFile(msg){ 
    alert(msg); 
} 

function file_transfer(fileEntry, uri, readBinaryData) { 

    var fileTransfer = new FileTransfer(); 
    var fileURL = fileEntry.toURL(); 

    fileTransfer.download(
     uri, 
     fileURL, 
     function (entry) { 
      alert("download complete: " + entry.toURL()); 

      if (readBinaryData) { 
       // Read the file... 
       readBinaryFile(entry); 
      } else { 
       // Or just display it. 
       displayImageByFileURL(entry); 
      } 

     }, 
     function (error) { 
      alert("download error source " + error.source); 
      alert("download error target " + error.target); 
      alert("upload error code" + error.code); 
     }, 
     null, // or, pass false 
     { 
      //headers: { 
      // "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" 
      //} 
     } 
    ); 
} 

在这种尝试中,我得到的警告信息“下载完整的:文件:///data/user/0/com.companyname.xxxxxxx/cache/downloaded -image.png“,但我也无法在设备中的任何位置找到图片。

我已经在两个不同的android设备上试过应用程序。

+0

我建议为这样的东西创建自己的科尔多瓦插件。我有很多的android开发和处理文件的正确创建是一个时间的B字,我不会相信他们的插件。 –

回答

0

你应该改变行

window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, 
-> 
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 

和 如果下载成功,你应该重新扫描设备存储,因为如果文件被下载科尔多瓦不知道。

所以我做了一个插件,

这是下载后更新画廊的插件。

https://github.com/pouu69/cordova-plugin-gallery-refresh