Skip to main content

Save Image/File into sdcard in Android using PhoneGap


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

Popular posts from this blog

File Upload & Download With ng-cordova File Transfer Plugin In Ionic Framework

Using the AngularJS extension set , ngCordova , with Ionic Framework and the Apache Cordova File Transfer plugin, you can easily upload files to a remote server and download files from a remote server. 1.Create the project ionic start Test blank cd Test ionic platform add android 2.Add Plugin org.apache.cordova.file-transfer https://github.com/apache/cordova-plugin-file-transfer This plugin allows you to upload and download files. This plugin defines global FileTransfer, FileUploadOptions Constructors. Although in the global scope, they are not available until after the deviceready event . Installation cordova plugin add cordova plugin add cordova-plugin-file-transfer     2.    org.apache.cordova.file             https://github.com/apache/cordova-plugin-file             This plugin implements a Fi...

What exactly means MVW design pattern ?

What is a MVW framework? The abbreviation stands for 'Model - View - Whatever'.  Well there are many different JavaScript frameworks available , all invented for the same purpose. They all try to separate the presentation logic from the business logic where JavaScript holds the model and logic, and html the presentation layer. Quick overview : You can change the model without changing the view and vice-versa Unit testing is easy These are the core benefits of using such a framework. By separating presentation and business logic changes to the code are easier to implement and take less time. Another benefit is that code can be reused from a controller thus saving more time. Angularjs makes the code also shorter in comparison to other frameworks, which improves code stability. Less code means minor potential for bugs. For several years +AngularJS was closer to MVC (or rather one of its client-side variants), but over time and thanks to many refactorings...

Open Existing SQLite File In PhoneGap for Android and iOS

Step 1 :  Just Open command prompt and  Create the App Go to the directory where you maintain your source code, and run a command such as the following: cordova create hello com.example.hello HelloWorld Step 2 :  Add Platform All subsequent commands need to be run within the project's directory, or any subdirectories within its scope: Before you can build the project, you need to specify a set of target platforms. Your ability to run these commands depends on whether your machine supports each SDK, and whether you have already installed each SDK. cordova platform add android { (optional with working multi platforms) Run this to check your current set of platforms: cordova platforms ls Run either of the following synonymous commands to remove a platform: cordova platform rm android } Step 3 : Just Open command prompt and go to your project path and install plugin using CLI cordova plugin add https://github.com/brodysoft/Cordova-...