Phonegap相机和捕获重启应用程序

时间:2015-03-26 12:07:31

标签: android cordova

我正在我的应用程序中实现照片上传功能, 用户可以使用相机或相册以两种不同的方式上传照片。

主要问题是当我打开相机或相册时,应用程序重新启动。 我使用了不同的前景摄像头插件,但问题没有解决。

当相机或相册打开时,应用会自动暂停和恢复, 我已经在我的应用中使用了简历事件,当应用恢复时会打开主页。

完成图片上传后,我需要转到上一页而不打开主页。 我使用的是cordova 3.6.4版本。

function captureImage() {
        navigator.device.capture.captureImage(captureSuccess, captureError, {limit: 1                    
});
    //window.history.back();
}
function captureSuccess(mediaFiles) {
        //alert("###1");
        var i, len;
        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
            uploadFile(mediaFiles[i]);
        }
}
function uploadFile(mediaFile) {
        //alert("###2");
        var ft = new FileTransfer(),
            path = mediaFile.fullPath,
            name = mediaFile.name;
                //alert("image path "+path);


       ft.upload(path,
            "http://my.domain.com/upload.php",
            function(result) {
                console.log('Upload success: ' + result.responseCode);
                console.log(result.bytesSent + ' bytes sent');
            },
            function(error) {
                alert("image upload failed");
                console.log('Error uploading file ' + path + ': ' + error.code);
            },
            { fileName: name });
}

2 个答案:

答案 0 :(得分:0)

我使用此代码(不是我的)并且它适用于捕获照片并且它不会重新启动应用程序:

function capturePhoto(){
navigator.camera.getPicture(uploadPhoto,null,{sourceType:1,quality:60});
}


function onPhotoDataSuccess(imageData) {
// Get image handle
//
    var smallImage = document.getElementById('cameraPic');
// 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
//
function onPhotoFileSuccess(imageData) {
// Get image handle
    console.log(JSON.stringify(imageData));
// Get image handle
//
    var smallImage = document.getElementById('cameraPic');
// 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
//
function onPhotoURISuccess(imageURI) {
// Uncomment to view the image file URI
// console.log(imageURI);
// Get image handle
//
    var largeImage = document.getElementById('cameraPic');
// 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

function capturePhotoWithData() {
// Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 30, correctOrientation: true });
}

function capturePhotoWithFile() {
navigator.camera.getPicture(onPhotoFileSuccess, onFail, { quality:50,  destinationType: Camera.DestinationType.FILE_URI,     correctOrientation: true });
}

// A button will call this function

function getPhoto(source) {
// Retrieve image file location from specified source
    navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 30,destinationType: destinationType.FILE_URI,
        correctOrientation: true });
}
// Called if something bad happens.
//
function onFail(message) {
alert('Failed because: ' + message);
}

答案 1 :(得分:0)

  var pictureSource;   // picture source
  var destinationType; // sets the format of returned value
  pictureSource=navigator.camera.PictureSourceType;
  destinationType=navigator.camera.DestinationType;

$(document).on('click','.capture_photo',function(){
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
              quality : 75,
              destinationType : Camera.DestinationType.DATA_URL,
              sourceType : Camera.PictureSourceType.CAMERA,
              // allowEdit : true,
              encodingType: Camera.EncodingType.PNG,
              // targetWidth: 100,
              // targetHeight: 100,
              popoverOptions: CameraPopoverOptions,
              saveToPhotoAlbum: false 
            });
        }); 


        function onPhotoDataSuccess(imageData) { 
          sessionStorage.setItem("img_api",imageData);
          $('#captureimg').attr('src','data:image/jpeg;base64,' + imageData);
        }

        $(document).on('click','.select_gallery',function(){
          navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
            encodingType: Camera.EncodingType.PNG,
            destinationType: destinationType.DATA_URL,
            sourceType: pictureSource.SAVEDPHOTOALBUM });
        });

        function onFail(message) {
           alert('Failed because: ' + message);
        }

希望,这会有所帮助