PhoneGap的 - 从以下网址保存图像到设备的照片库图像、网址、照片、设备

2023-09-12 00:12:52 作者:灰飛煙滅的誓言

我开发PhoneGap的应用程序,我需要从URL保存图像的设备照片库。

I'm developing phonegap application and I need to save Image from url to the Device Photo Gallery.

我找不到在PhoneGap的做它,我也没有发现PhoneGap的插件,该API的方式。

I can't find at the Phonegap Api a way for doing it and Also I didn't find phonegap plugin for that.

我需要它与iPhone和功放工作;安卓

I need it to work with Iphone & Android

非常感谢!

推荐答案

这是文件下载code,可以被任何人使用。你只需要三个参数,使用这个像 -

This is file download code which can be used by anyone. You just have three parameters to use this like-

1)的网址

2)的文件夹名称要建立在你的SD卡

2) Folder name which you want to create in your Sdcard

3)文件名(你可以给你的任何文件名)

3) File name (You can give any name to file)

所有类型的文件都可以使用此code下载。你可以使用它作为.js文件 这适用于 IOS 也。

All types of file can download by using this code. you can use this as .js And this works on IOS also.

//First step check parameters mismatch and checking network connection if available call    download function
function DownloadFile(URL, Folder_Name, File_Name) {
//Parameters mismatch check
if (URL == null && Folder_Name == null && File_Name == null) {
    return;
}
else {
    //checking Internet connection availablity
    var networkState = navigator.connection.type;
    if (networkState == Connection.NONE) {
        return;
    } else {
        download(URL, Folder_Name, File_Name); //If available download function call
    }
  }
}

//第二步获得写入权限和创建文件夹

//Second step to get Write permission and Folder Creation

function download(URL, Folder_Name, File_Name) {
//step to request a file system 
    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.fullPath; // Returns Fulpath of local directory

    fp = fp + "/" + Folder_Name + "/" + File_Name + "." + ext; // fullpath and name of the file which we want to give
    // download function call
    filetransfer(download_link, fp);
}

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

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

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

下载//第三步一个文件到创建的文件夹

//Third step for download a file into created folder

function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(download_link, fp,
                    function (entry) {
                        alert("download complete: " + entry.fullPath);
                    },
                 function (error) {
                     //Download abort errors or download failed errors
                     alert("download error source " + error.source);
                     //alert("download error target " + error.target);
                     //alert("upload error code" + error.code);
                 }
            );
}

相关链接