Using Device Features in Your Touch App

In this guide, we will walk through the process of creating a simple native Sencha Touch application that can access an Android device’s photo library and camera. Within the application, you may:

  • Select an image from the photo library
  • Take a new picture using the camera.

The resulting image will then appear in the application’s main panel.

Note: This guide only applies to Androids using Cordova.

Prerequisites

The following tools must be installed before proceeding:

Generate Your Touch application

Open your CLI and generate a starter application with Sencha Cmd by issuing the following:

sencha -sdk /path/to/sdk generate app touchCamera /target/app/directory

Note: Please utilize ‘touchCamera’ as your application name or be prepared to modify the provided code as needed.

Update Application with Example Code

Using your editor of choice, replace the contents of the “app/view/Main.js” file in your application’s view directory. Your new code should look like this:

Ext.define('touchCamera.view.Main', {
    extend: 'Ext.Panel',
    xtype: 'main',
    requires: [
        'Ext.Button',
        'Ext.Img'
    ],
    config: {
        layout: {
            type:"vbox",
            pack:"center",
            align:"center"
        },
        items: [{
            xtype: 'titlebar',
            docked: 'top',
            title: 'Native API Examples'
        }, {
            xtype: "image",
            src: "http://placehold.it/200x200",
            width: '100%',
            height: '100%'
        }, {
            xtype: 'toolbar',
            docked: 'bottom',
            items: [{
                xtype: "button",
                text: "Photo Library",
                handler: function(btn) {
                    var panel = btn.up('panel');

                    panel.getPhoto(navigator.camera.PictureSourceType.PHOTOLIBRARY);
                }
            }, {
                xtype: "button",
                text: "Take Photo",
                handler: function(btn) {
                    var panel = btn.up('panel');

                    panel.getPhoto(navigator.camera.PictureSourceType.CAMERA);
                }
            }]
        }]
    },

    getPhoto: function(source) {
        var me = this;

        navigator.camera.getPicture(me.success, me.failure, {
            quality: 50,
            destinationType: navigator.camera.DestinationType.FILE_URI,
            sourceType: source 
        });

    },

    success: function(image_uri) {
        var img = Ext.ComponentQuery.query("image")[0];
        img.setSrc(image_uri);
    },

    failure: function(message) {
        alert("Failed" + message);
    }
});

Create & Set Up the Cordova Environment

The next step is to create a Cordova project in the application folder. Cordova can access the native Android APIs so that the application can interact with the device photo library and camera.

  1. Change to the directory in which the application was generated by ‘sencha generate app’.

    cd /target/app/directory
  2. Create a Cordova project in your application directory.

    sencha cordova init

    You should now see that a Cordova directory has been created in your application’s directory. The default configuration files created for the project will be need to be updated as per the device APIs your application needs to access. You must also include the device permissions needed for your application to function properly.

    This process also updates the app.json file in your application to include a “builds” configuration block. This block needs to be updated to reflect the build type your application is creating.

  3. Edit app.json file and update the “builds” block to look like the following example.

    Note: In the case below, we are specifying a new build target of ‘android’ that will use the Cordova packager and run on an android platform:

    "builds": {
           "web": {"default": true},
           "android": { // This can be any build target name you desire (eg myApp, apple, etc)
                "packager": "cordova", // Specify that we are using cordova to package the app
                "cordova" : { // configuration for cordova build
                    "config": {
                         "id": "com.domain.touchCamera",  // namespace of application (required)
                         "name": "touchCamera",  // app name which appears under the icon on device
                         "platforms": "android" // add platform(s) here
                    }
               }
            }
        },
  4. CD into the “cordova” directory

    cd cordova
  5. Each API that is used in the application requires its corresponding plugin is added to the project. In this example, the camera is used. To add the camera plugin, issue the following command in the “cordova” directory :

    cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-camera.git
  6. Verify that the plugin has been added to the “cordova” project:

    cordova plugin list 
    //org.apache.cordova.camera 0.3.4-dev "Camera"
  7. Next, let’s tell Cordova that the application will be using the camera plugin by adding the following information to the config.xml file:

    <feature name="Camera"> 
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" /> 
    </feature>
  8. Add write permissions to the AndroidManifest.xml file found in “cordova/platforms/android” so the camera can save pictures to the device. The following line will need to be added to the file directly after line regarding android.permission.INTERNET.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Build your Android Package

Once the configuration files have been updated with the appropriate permissions and feature information, change back to the primary application directory and build the application:

cd ..
sencha app build android

A successful build will result in the creation of an Android Package (APK) found here:

cordova/platforms/android/ant-build

You can verify its existence with the following command:

find . name -name *.apk
//./cordova/platforms/android/ant-build/touchCamera-debug-unaligned.apk
//./cordova/platforms/android/ant-build/touchCamera-debug.apk

Connecting the Device

Prior to installation, please make sure that the:

  • Android device is connected
  • Android debugging has been enabled
  • Android device has been authorized for the computer to which it is connected.

Next, ensure that the development computer can see the attached Android device by issuing the following command:

adb devices
//* daemon not running. starting it now on port 5037 *
//* daemon started successfully *
//List of devices attached 
//015d262e99100202  device

The above output shows one Android device connected to a computer.

Note: Your device number will be different than the one found above.

If you do not see a device listed, please check your connection, device permissions, and debugging status. Installing the Application

Move the package to the Android device by issuing the following command:

$ adb install -r cordova/platforms/android/ant-build/touchCamera-debug.apk 
//1284 KB/s (2446896 bytes in 1.860s)
//pkg: /data/local/tmp/touchCamera-debug.apk
//Success

Launch the Touch Application

If the previous adb install command executed successfully, the Touch application will appear in the apps section of the Android device. The standard Cordova icon will appear and the name under the icon will be touchCamera. Tap the icon to launch the Touch application.

The application will have two buttons at the bottom of the screen. The first opens the photo library and second launches the camera.

Touch either button to see the app in action. After returning from either the photo library or camera, the selected picture will appear in the image block in the middle of the application.

Last updated