【问题标题】:Phonegap camera and capture restart appPhonegap 相机和捕获重新启动应用程序
【发布时间】:2015-03-27 16:56:38
【问题描述】:

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

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

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

我需要在完成图片上传后转到上一页而不打开主页。 我使用的是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 });
}

【问题讨论】:

  • 你能把你的代码贴在这里吗?

标签: android cordova


【解决方案1】:

我使用此代码(不是我的),它可以很好地拍摄照片,并且不会重新启动应用程序:

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);
}

【讨论】:

    【解决方案2】:
      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);
            }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 2020-09-30
      • 2013-04-09
      相关资源
      最近更新 更多