Phonegap - 将图像从网址保存到设备照片库中我正在开发phonegap应用程序,我需要将图像从URL保存到设备照片库。我在Phonegap Api上找不到这样做的方法,而且我没有为此找到phonegap插件。我需要它与Iphone和Android合作非常感谢!
3 回答
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
这是任何人都可以使用的文件下载代码。你只需要三个参数来使用它 -
1)URL
2)要在Sdcard中创建的文件夹名称
3)文件名(您可以为文件指定任何名称)
所有类型的文件都可以使用此代码下载。你可以使用它作为.js这IOS也适用。
//First step check parameters mismatch and checking network connection if available call download functionfunction DownloadFile(URL, Folder_Name, File_Name) {//Parameters mismatch checkif (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
}
}}//获取写入权限和文件夹创建的第二步
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);
}}//将文件下载到创建文件夹的第三步
function filetransfer(download_link, fp) {var fileTransfer = new FileTransfer();// File download function with URL and local pathfileTransfer.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);
}
);}
largeQ
TA贡献2039条经验 获得超8个赞
最新版本的Cordova(3.3 +),较新的(1.0.0+)版本的File使用文件系统URL而不是文件路径。因此,要使接受的答案与FileSystemSuccess函数中的较新版本一起使用,请更改以下行:
var fp = rootdir.fullPath;
至:
var fp = rootdir.toURL();
添加回答
举报
0/150
提交
取消
