Iphone 如何使用cordova 3.3.0将照片从照相机和多媒体资料上传到服务器

Iphone 如何使用cordova 3.3.0将照片从照相机和多媒体资料上传到服务器,iphone,cordova-3,Iphone,Cordova 3,我正在学习cordova 3.3.0版。我用这段代码在iphone上打开相机和画廊,但没有成功 我的代码: <script type="text/javascript"> var pictureSource; // picture source var destinationType; // sets the format of returned value // Wait for device API libraries

我正在学习cordova 3.3.0版。我用这段代码在iphone上打开相机和画廊,但没有成功

我的代码:

    <script type="text/javascript">
        var pictureSource;   // picture source
        var destinationType; // sets the format of returned value

        // Wait for device API libraries to load
        //
        document.addEventListener("deviceready",onDeviceReady,false);

        // device APIs are available
        //
        function onDeviceReady() {
            pictureSource=navigator.camera.PictureSourceType;
            destinationType=navigator.camera.DestinationType;
        }

    // Called when a photo is successfully retrieved
    //
    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 in-line CSS rules are used to resize the image
        //
        smallImage.src = "data:image/jpeg;base64," + 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('largeImage');

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

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

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

    // 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(onPhotoDataSuccess, onFail, { quality: 20, allowEdit: true,
                                    destinationType: destinationType.DATA_URL });
    }

    // A button will call this function
    //
    function getPhoto(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.
    //
    function onFail(message) {
        alert('Failed because: ' + message);
    }

    </script>

var pictureSource;//图像源
var destinationType;//设置返回值的格式
//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
函数ondevicerady(){
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.destinationType;
}
//成功检索照片时调用
//
函数onPhotoDataSuccess(imageData){
//取消注释以查看base64编码的图像数据
//控制台日志(imageData);
//获取图像句柄
//
var smallImage=document.getElementById('smallImage');
//取消隐藏图像元素
//
smallImage.style.display='block';
//显示捕获的照片
//内嵌CSS规则用于调整图像大小
//
smallImage.src=“数据:图像/jpeg;base64,”+imageData;
}
//成功检索照片时调用
//
函数onPhotoURISuccess(imageURI){
//取消注释以查看图像文件URI
//log(imageURI);
//获取图像句柄
//
var largeImage=document.getElementById('largeImage');
//取消隐藏图像元素
//
largeImage.style.display='block';
//显示捕获的照片
//内嵌CSS规则用于调整图像大小
//
largeImage.src=imageURI;
}
//一个按钮将调用此函数
//
函数capturePhoto(){
//使用设备摄像头拍照,并将图像作为base64编码字符串检索
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{质量:50,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数capturePhotoEdit(){
//使用设备照相机拍照,允许编辑,并以base64编码字符串的形式检索图像
navigator.camera.getPicture(onPhotoDataSuccess,onFail,{quality:20,allowEdit:true,
destinationType:destinationType.DATA_URL});
}
//一个按钮将调用此函数
//
函数getPhoto(源代码){
//从指定源检索图像文件位置
navigator.camera.getPicture(onPhotoURISuccess,onFail,{质量:50,
destinationType:destinationType.FILE\u URI,
sourceType:source});
}
//如果有什么不好的事情发生了就打电话。
//
函数onFail(消息){
警报('失败原因:'+消息);
}
有谁能指导我如何使用cordova打开相机和画廊,就像我们在native中打开一样


提前感谢。

请确认您已仔细创建项目

1. included cordova.js in html page
2. added xml folder containing config.html to res folder
3. added required permissions in android project manifest file.

if everything is ok, it should work. then check for javascript error (use try and catch clock)
请看我的代码

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady() {
            pictureSource=navigator.camera.PictureSourceType;
             destinationType=navigator.camera.DestinationType;
     }

  function uploadFromGallery(){
                 navigator.camera.getPicture(uploadPhoto, function(message){
                 console.log("get picture failed");
                  },{ quality: 75,destinationType:         navigator.camera.DestinationType.FILE_URI,sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY,allowEdit:true} );
                                   }




function uploadPhoto(imageURI) {
               global_URI=imageURI;
                var largeImage = document.getElementById('largeImage');
               // Unhide image elements
                largeImage.style.display = 'block';
                // Show the captured photo
                  largeImage.src = imageURI;
               }


 function capturePhoto() {
          largeImage.src='';
          // Take picture using device camera and retrieve image as base64-encoded string
                                             navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 75,destinationType: destinationType.FILE_URI,saveToPhotoAlbum: true, allowEdit:true});

                                   }


     function onPhotoDataSuccess(imageURI) {
                // Uncomment to view the base64-encoded image data
                // Get image handle
                global_URI=imageURI;
               var largeImage = document.getElementById('largeImage');
                 // Unhide image elements

                 largeImage.style.display = 'block';
                // Show the captured photo
                   largeImage.src = imageURI;

      }

function uploadimageonserver (){

var options = new FileUploadOptions();
   options.fileKey="image";
                                                          options.fileName=global_URI.substr(global_URI.lastIndexOf('/')+1);
  options.mimeType="image/jpeg";
                                                           var params = new Object();
                                                            options.params = params;
                                                            params.user_id =userID;
                                                            params.post_id=postId;
                                                            var ft = new FileTransfer();
                                                            ft.upload(global_URI, encodeURI(ipAddress +"/post_comment.php"), win, fail, options);
}

function win(r) {
                                                var result = JSON.parse(r.response); alert(result);

}

  function fail(error) {
                                                    var err="An error has occurred: Code ="+error.code;
}

这是我遵循的链接:——我正在为ios开发