Thursday, April 3, 2014

How to add a configuration file and access it values in a PhoneGap/Cordova Application

Add a xml file to the asset/www folder and give it a name(config.xml in our case). Add settings that you need to include.

config.xml:


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings>
        <!-- QA Server-->
        <service url="http://whatsup.ai.com/service/api/" />
        <!-- Live Server-->
        <!--service url="https://user.whatsup.com/service/api/" /-->
        <version versionnumber="1.0.0.0" />
    </appSettings>

</configuration>


Code for accessing service url attribute of config file:

var serviceUrl;
function readServiceUrl() {
      $.ajax({
           url : "config.xml",
           dataType : "html",
           success : function(xmlResponse) {
           serviceUrl = $(xmlResponse).find('service').attr('url');
          
           },
           error : function(error) {
           console.log(error);
           },
           async : false
           });
}

How to capture a photo from camera and copy it in to a local cache in a PhoneGap/Cordova Application

Following code illustrate how to capture a photo from camera and copy it in to a local cache and then use the copied file in the app. Methods from both Phonegap Camera API and File API are used. Variable ‘root’ referred to the Application root folder and how to get the app root prefer the previous post.

Code:


function capturePhoto() {
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 100, destinationType: Camera.DestinationType.FILE_URI,correctOrientation: true, saveToPhotoAlbum: false });
            }
            
//getting picture from camera succeeded          
function onPhotoURISuccess(imageURI) {
                createFileEntry(imageURI);
            }
            
 //getting picture from camera failed                     
function onFail(message) {
                console.log("Failed to load picture because: "+message);
              
            }
            
//get the image file           
function createFileEntry(imageURI) {
                window.resolveLocalFileSystemURI(imageURI, copyPhoto, fail);
            }
            
//copy image in the cache  
function copyPhoto(fileEntry) {
                var d = new Date();
                var n = d.getTime();
                //new file name with unique name
                var newFileName = n + ".jpg";
                
                root.getDirectory("ImageCache", {create: true, exclusive: false}, function(dir) {
                                  fileEntry.copyTo(dir, newFileName, onCopySuccess, fail);
                                  }, fail);
                                  
            }
            
//copying image succeeded
function onCopySuccess(entry) {
               
                 console.log(entry.name);
                 var image = document.getElementById('myImage');
     image.src = entry.fullPath;                
            }
            
//copying image failed           
function fail(error) {
                console.log(error.code);
            }

Wednesday, April 2, 2014

Create a Cache folder in the Project's root folder in a PhoneGap/Cordova Project.

When getting application root we have to use to different methods in different platforms. That’s because in Android you can access any folder and when called for the persistent storage root, from your application it will returns the root of the memory card or phone's persistent memory. But in iOS, when you called for persistent storage root from your application (like the code shown in below), it will returns the ‘Documents’ folder which is located right at where your app is installed in the phone memory.

Code:


var root;
var imageCache;           

//get the root of the application.
function getAppRoot() {

  if (device.platform == "Android") {
window.resolveLocalFileSystemURI("file:///data/data/com.geveo.myapp", onSuccess, onError);
        
  } else if (device.platform == "iOS") {
 window.requestFileSystem(LocalFileSystem.PERSISTENT,0, function(fileSystem) {
                                 root = fileSystem.root;
                                 onSuccess(root);
                                 }, function(evt) { 
                                   // error getting file system
                                   console.log(evt.target.error.code);
                                 });
}
}

//create cache if not exist iside root folder
function onSuccess(entry) {
root = entry;
root.getDirectory("ImageCache", {
                      create : true,
                      exclusive : false
                      }, onImageCacheCreateSuccess, onImageCacheCreateFail);
}

function onImageCacheCreateSuccess(dir) {
    
 imageCache = dir;
 console.log(dir.fullPath.toString());
}

function onImageCacheCreateFail(error) {
    console.log("ImageCache Create Fail"+error.code );
    
}

function onError(error) {
console.log("Error"+error.code );
}