Skip to content Skip to sidebar Skip to footer

How To Launch Camera From Button In Webview -android

How can I launch the camera of the device from a button in a Webview? I have this code in Android: protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved

Solution 1:

you can use Phonegap

extend Activity to DroidGap

then create folder www inside assets folder and inside folder www create folder js and pu inside it phonegap.js

then create file index.html:

<!DOCTYPE html><html><head><title>Capture Photo</title><metaname="viewport"content="width=device-width,height=device-height,initial-scale=1"/><scripttype="text/javascript"charset="utf-8"src="js/phonegap.js"></script><scripttype="text/javascript"charset="utf-8">var pictureSource;   // picture sourcevar destinationType; // sets the format of returned value // Wait for PhoneGap to connect with the device//document.addEventListener("deviceready",onDeviceReady,false);

    // PhoneGap is ready to be used!//functiononDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }

    // Called when a photo is successfully retrieved//functiononPhotoDataSuccess(imageData) {
      // Get image handle//var smallImage = document.getElementById('smallImage');

      // Unhide image elements//
      smallImage.style.display = 'block';

      // Show the captured photo// The inline CSS rules are used to resize the image//
      smallImage.src = "data:image/jpeg;base64," + imageData;
    }

    // Called when a photo is successfully retrieved//functiononPhotoFileSuccess(imageData) {
      // Get image handleconsole.log(JSON.stringify(imageData));

      // Get image handle//var smallImage = document.getElementById('smallImage');

      // Unhide image elements//
      smallImage.style.display = 'block';

      // Show the captured photo// The inline CSS rules are used to resize the image//
      smallImage.src = imageData;
    }

    // Called when a photo is successfully retrieved//functiononPhotoURISuccess(imageURI) {
      // Uncomment to view the image file URI // console.log(imageURI);// Get image handle//var largeImage = document.getElementById('largeImage');

      // Unhide image elements//
      largeImage.style.display = 'block';

      // Show the captured photo// The inline CSS rules are used to resize the image//
      largeImage.src = imageURI;
    }

    // A button will call this function//functioncapturePhotoWithData() {
      // Take picture using device camera and retrieve image as base64-encoded string
      navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
    }

    functioncapturePhotoWithFile() {
        navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
    }

    // A button will call this function//functiongetPhoto(source) {
      // Retrieve image file location from specified source
      navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
        destinationType: destinationType.FILE_URI,
        sourceType: source });
    }

    // Called if something bad happens.// functiononFail(message) {
      alert('Failed because: ' + message);
    }

    </script></head><body><buttononclick="capturePhotoWithData();">Capture Photo With Image Data</button><br><buttononclick="capturePhotoWithFile();">Capture Photo With Image File URI</button><br><buttononclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br><buttononclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br><imgstyle="display:none;width:60px;height:60px;"id="smallImage"src="" /><imgstyle="display:none;"id="largeImage"src="" /></body></html>

and after this you must add library cordova.jar

then run your application

Post a Comment for "How To Launch Camera From Button In Webview -android"