Cordova - 将 url 中的图像保存到设备照片库中
Cordova - Save image from url into device photo gallery
我正在使用 Apache Cordova 开发一个下载和保存图像的应用程序,但我无法保存和显示图库,图像暂时转到 file:///data/data
我正在 Android 上尝试 运行,我该怎么办?
我的代码:
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);
var ext = download_link.substring(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(); // Returns Fulpath of local directory
console.log(rootdir);
fp = fp + "/" + Folder_Name + "/" + File_Name; // fullpath and name of the file which we want to give
// download function call
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
//alert("Sucesso");
}
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();
console.log(fp);
// 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
console.log(error);
alert(error.exception);
alert("download error source " + error.source);
//alert("download error target " + error.target);
//alert("upload error code" + error.code);
}
);
}
正在保存图像,但需要 Media Scanner 为图库中的图像编制索引
媒体扫描器:
MediaScannerConnection provides a way for applications to pass a newly
created or downloaded media file to the media scanner service. The
media scanner service will read metadata from the file and add the
file to the media content provider.
使用 Apache Cordova / PhoneGap 时如何?它没有提供刷新本地图库中图像的本地方法,我不得不寻找一个插件来完成这项工作。我找到的插件是:
cordova-mediascanner-plugin
MediaScannerPlugin
两者都有基本的文档,但是我使用了 cordova-mediascanner-plugin。
通过实现这个插件,只是修改了我的 filetransfer
方法
function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
console.log(fp);
// File download function with URL and local path
fileTransfer.download(download_link, fp,
function (entry) {
//alert("download complete: " + entry.fullPath);
window.plugins.scanmedia.scanFile(fp, function (msg) {
alert("Success ScanMedia");
}, function (err) {
alert("Fail ScanMedia: " + err);
})
},
function (error) {
//Download abort errors or download failed errors
console.log(error);
alert(error.exception);
alert("download error source " + error.source);
//alert("download error target " + error.target);
//alert("upload error code" + error.code);
}
);
}
请尝试在您的项目中导入此插件:-
我正在使用 Apache Cordova 开发一个下载和保存图像的应用程序,但我无法保存和显示图库,图像暂时转到 file:///data/data
我正在 Android 上尝试 运行,我该怎么办?
我的代码:
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);
var ext = download_link.substring(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(); // Returns Fulpath of local directory
console.log(rootdir);
fp = fp + "/" + Folder_Name + "/" + File_Name; // fullpath and name of the file which we want to give
// download function call
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
//alert("Sucesso");
}
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();
console.log(fp);
// 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
console.log(error);
alert(error.exception);
alert("download error source " + error.source);
//alert("download error target " + error.target);
//alert("upload error code" + error.code);
}
);
}
正在保存图像,但需要 Media Scanner 为图库中的图像编制索引
媒体扫描器:
MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.
使用 Apache Cordova / PhoneGap 时如何?它没有提供刷新本地图库中图像的本地方法,我不得不寻找一个插件来完成这项工作。我找到的插件是:
cordova-mediascanner-plugin
MediaScannerPlugin
两者都有基本的文档,但是我使用了 cordova-mediascanner-plugin。
通过实现这个插件,只是修改了我的 filetransfer
方法
function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
console.log(fp);
// File download function with URL and local path
fileTransfer.download(download_link, fp,
function (entry) {
//alert("download complete: " + entry.fullPath);
window.plugins.scanmedia.scanFile(fp, function (msg) {
alert("Success ScanMedia");
}, function (err) {
alert("Fail ScanMedia: " + err);
})
},
function (error) {
//Download abort errors or download failed errors
console.log(error);
alert(error.exception);
alert("download error source " + error.source);
//alert("download error target " + error.target);
//alert("upload error code" + error.code);
}
);
}
请尝试在您的项目中导入此插件:-