Mostly we call webservice and pass data as json format and we can easily fetch data for android application but sometimes when you fetching image from server it’s take much time to load and even if no internet,image can’t be load.
So, To cache image or to save loading time of image from server in every page load, we can store image into local. It’s very easy to store image into local using PhoneGap.
Let’s understand the code:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
//request the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess,
fileSystemFail);
}
function fileSystemSuccess(fileSystem) {
var directoryEntry = fileSystem.root; // to get root path to directory
directoryEntry.getDirectory("<folder_name>", {create: true, exclusive: false},
onDirectorySuccess, onDirectoryFail);
var rootdir = fileSystem.root;
var fp = rootdir.fullPath;
fp = fp+"/<folder_name>/image_name.png";
var fileTransfer = new FileTransfer();
fileTransfer.download("<url_to_download>",fp,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
}
);
}
function onDirectorySuccess(parent) {
console.log(parent);
}
function onDirectoryFail(error) {
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
console.log(evt.target.error.code);
}
Some time Error occurred like : Source URL is not in white list: 'http://abc//santosh.png
Solution :
Change your cordova.xml or phonegap.xm in project directory res>xml>cordova.xml
<access origin="http://127.0.0.1*"/>
Replaced By :
<access origin="*" subdomains="true"/>
This white-lists basically the entire internet. If you only want to white-list your domain, use something more specific. See details here:
Comments
Post a Comment