Back to Top

How to Save File in SD card using PhoneGap?

Save Image/File into sdcard in Android using Phonegap

Today I am going to explain about how to save an external image into your device’s SDCard in android using PhoneGap. PhoneGap is a framework for building mobile apps using HTML, JavaScript and CSS and known as Cordova.

Mostly we call web service and pass data as JSON format and we can easily fetch data for android application but sometimes when you fetching image from server it takes much time to load and even if no internet, image can’t be load.

[sociallocker]

So, To cache image or to save loading time of image from the server in every page load, we can store an image into local. It’s very easy to store an image into local using Phonegap.

Also READ: To setup SQLite for Android-Phonegap application

Let’s understand the code:


document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
//request the persistent file system
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
}

Here, I have called requestFileSystem which will create new storage for your app.I have used a LocalFileSystem.PERSISTENT file system which is not removed at browser’s discretion.There is also LocalFileSystem.TEMPORARY. The second argument is the size in bytes.

Must READ: To use JSON Resonse of Webservice in Phonegap Android


function fileSystemSuccess(fileSystem) {
    var directoryEntry = fileSystem.root; // to get root path to directory
    directoryEntry.getDirectory("", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail);
    var rootdir = fileSystem.root;
    var fp = rootdir.fullPath;
    fp = fp+"//image_name.png";
    var fileTransfer = new FileTransfer();
   fileTransfer.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);
}

That’s it. Above code will help you to store an image into SDCard using PhoneGap.

As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates. [/sociallocker]

Comments (1)

  1. This will only save file to phone’s internal storage not in physical sd card.

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);

    will give a filesystem whose fullPath = “/”. But if you check the directory entry of this directoryEntry it will display the content of phone’s internal storage, not the content of the sd card.

    I’m wondering what phone did you test this on. I’ve tried it on 2 samsung and 1 alcatel and it behaved as I described it above.

    I am still looking for a way to make this work though.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to Use PHP CodeSniffer?

Posted on 8 years ago

Bhumi

How to convert RGB to Hex color PHP?

Posted on 12 years ago

Bhumi

How To set up Signup Bonus in X-Cart

Posted on 13 years ago

Bhumi