0

I have below code in my app to upload picture (Using camera or select from gallery). It works fine in my Google Nexus tab 4.3.0 but it crashes in Samsung mobile 4.1.2 as soon as I take picture or select a picture from Gallery. I tried make many changes as suggested on internet but none worked. Any idea?

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

var deviceReady = false;

/**
 * Take picture with camera
 */
function takePicture() {
    navigator.camera.getPicture(
        function(uri) {
            var img = document.getElementById('camera_image');
            img.style.visibility = "visible";
            img.style.display = "block";
            img.src = uri;
            document.getElementById('camera_status').innerHTML = "Success";
        },
        function(e) {
            console.log("Error getting picture: " + e);
            document.getElementById('camera_status').innerHTML = "Error getting picture.";
        },
        { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI});
};

/**
 * Select picture from library
 */
function selectPicture() {
    navigator.camera.getPicture(
        function(uri) {
            var img = document.getElementById('camera_image');
            img.style.visibility = "visible";
            img.style.display = "block";
            img.src = uri;
            document.getElementById('camera_status').innerHTML = "Success";
        },
        function(e) {
            console.log("Error getting picture: " + e);
            document.getElementById('camera_status').innerHTML = "Error getting picture.";
        },
        { quality: 50, destinationType: navigator.camera.DestinationType.FILE_URI, sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY});
};

/**
 * Upload current picture
 */
function uploadPicture() {

    // Get URI of picture to upload
    var img = document.getElementById('camera_image');
    var imageURI = img.src;
    if (!imageURI || (img.style.display == "none")) {
        document.getElementById('camera_status').innerHTML = "Take picture or select picture from library first.";
        return;
    }

    // Verify server has been entered
    server = document.getElementById('serverUrl').value;
    if (server) {

        // Specify transfer options
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpeg";
        options.chunkedMode = false;

        // Transfer picture to server
        var ft = new FileTransfer();
        ft.upload(imageURI, server, function(r) {
            document.getElementById('camera_status').innerHTML = "Upload successful: "+r.bytesSent+" bytes uploaded.";              
        }, function(error) {
            document.getElementById('camera_status').innerHTML = "Upload failed: Code = "+error.code;               
        }, options);
    }
}

/**
 * View pictures uploaded to the server
 */
function viewUploadedPictures() {

    // Get server URL
    server = document.getElementById('serverUrl').value;
    if (server) {

        // Get HTML that lists all pictures on server using XHR 
        var xmlhttp = new XMLHttpRequest();

        // Callback function when XMLHttpRequest is ready
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState === 4){

                // HTML is returned, which has pictures to display
                if (xmlhttp.status === 200) {
                    document.getElementById('server_images').innerHTML = xmlhttp.responseText;
                }

                // If error
                else {
                    document.getElementById('server_images').innerHTML = "Error retrieving pictures from server.";
                }
            }
        };
        xmlhttp.open("GET", server , true);
        xmlhttp.send();         
    }   
}

/**
 * Function called when page has finished loading.
 */
function init() {
    document.addEventListener("deviceready", function() {deviceReady = true;}, false);
    window.setTimeout(function() {
        if (!deviceReady) {
            alert("Error: PhoneGap did not initialize.  Demo will not run correctly.");
        }
    },2000);
}

</script>
  • 1
    You're asking people to review a lot of code and try to spot a bug. It would help a lot if you knew what function the crash occurred in. Have you tried removing parts of the code that aren't relevant to the bug until you have the smallest amount of code that still works on one device and crashes on the other? – perfectionist Nov 02 '13 at 07:34

1 Answers1

1

I got the same issue and I resolved it by desactivating THE DEVELOPER OPTION, or uncheck "Do not keep activities", because that kills the app once the camera is launched and it cant resume it again.

You can refer to this answer : Phonegap Capture not working for jelly bean

Community
  • 1
  • 1
ghost rider3
  • 448
  • 1
  • 5
  • 17