4

I captured image using PhoneGap using the camera.getPicture. Now, I cannot get to move/copy that captured image to a location of my choice. I tried using the File API of PhoneGap but I could not succeed. Could anyone please tell me what is wrong in the below code?

PS : I am running this app in Android Galaxy S2. The photo is being saved in the default photo gallery.

<script type="text/javascript" charset="utf-8">

        var pictureSource;   // picture source
        var destinationType; // sets the format of returned value 
        var photoid=window.localStorage.getItem("photoid");
        var photoData=null;
        // Wait for PhoneGap to connect with the device
        //
        document.addEventListener("deviceready",onDeviceReady,false);

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

        }
        function capturePhoto() {
            // Take picture using device camera and retrieve image as base64-encoded string
            navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50 });
        }

        // A button will call this function
        //
        function capturePhotoEdit() {
            // Take picture using device camera, allow edit, and retrieve image as base64-encoded string  
            navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 20, allowEdit: true }); 
        }

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

        function onPhotoDataSuccess(imageData) {
            // Uncomment to view the base64 encoded image data
            // console.log(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;
            alert(imageData);
            photoData = imageData;
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
        }

        function gotFS(fileSystem) {
            fileSystem.root.getFile("/sdcard/external_sd/sastphotos/"+photoid+".jpg", null, gotFileEntry, fail);
        }

        function gotFileWriter(writer) {
            writer.onwrite = function(evt) {
                alert("write success");
            };
            writer.write(photoData);
        }

        function fail(error) {
            alert(error.code);
        }


        function onPhotoURISuccess(imageURI) {
            // Uncomment to view the image file URI 
            // console.log(imageURI);
            //alert("photo captured");
            uploadPhoto(imageURI);
        }

        function getPhoto(source) {
            // Retrieve image file location from specified source
            navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50, 
                                        destinationType: destinationType,
                                        sourceType: source });
        }

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

        </script>
Sonia John Kavery
  • 2,099
  • 2
  • 20
  • 36
ilight
  • 1,622
  • 2
  • 22
  • 44
  • Do you have external storage write permission in your application manifest file? – bluefalcon Dec 09 '11 at 11:42
  • I have developed the app in XCode and then got the APK from build.phonegap.com. Will try in eclipse and let you know. thanks for the suggestion. – ilight Dec 10 '11 at 16:49
  • Yes, I have the write permission to external storage. Can you suggest how I can move the captured image to a different location? – ilight Dec 16 '11 at 01:12
  • Hi, i am tryin to extract all the image files from sd card. so can you tell me what is the source in getPhoto(source). I am new to phonegap so kindly help. – Khush Jan 18 '12 at 03:33
  • See http://stackoverflow.com/questions/10335563/capturing-and-storing-a-picture-taken-with-the-camera-into-a-local-database-ph . – Juho Vepsäläinen Mar 22 '13 at 14:16

0 Answers0