Phonegap - 将图像从 url 保存到设备照片库中 [英] Phonegap - Save image from url into device photo gallery

查看:24
本文介绍了Phonegap - 将图像从 url 保存到设备照片库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 phonegap 应用程序,我需要将图像从 url 保存到设备照片库.

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

我在 Phonegap Api 上找不到这样做的方法,而且我也没有找到用于此的 phonegap 插件.

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

非常感谢!

推荐答案

这是任何人都可以使用的文件下载代码.您只需使用三个参数即可 -

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)

使用此代码可以下载所有类型的文件.您可以将其用作 .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);
                 }
            );
}

有用链接

这篇关于Phonegap - 将图像从 url 保存到设备照片库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆