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:
1 2 3 4 5 6 7 8 | document.addEventListener("deviceready", onDeviceReady, false); //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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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(); function(entry) { }, alert("download error source " + error.source); alert("download error target " + error.target); alert("upload error code" + error.code); } ); } 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)