PhoneGap的:捕获图像,视频和发送附件的电子邮件 [英] Phonegap : capture Image, Video and Send as Attachment in Email

查看:227
本文介绍了PhoneGap的:捕获图像,视频和发送附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从安卓/ iOS设备拍摄的图像和视频,并通过电子邮件发送给亲友。 我想打一个混合的应用程序。所以我用PhoneGap的最新版本为。 我搜索互联网,发现一些code和安排他们在一起。现在,我希望用户能够捕获只有2视频和我的code对于工作的罚款。但现在当我尝试捕捉图像,然后它不工作。图片不在本地存储。我也希望用户能够捕获只有5图像,当点击发送按钮时,拍摄的图像和连接并通过电子邮件发送视频。 这是我的code,我做的是什么

I try to capture image and video from android/ Ios device and send them through email. I Want to make a hybrid app. so I use phonegap latest release for that. I search over Internet and found some of code and arrange them together. Now I want that user can capture only 2 videos, and my code for that working fine. but now when I try to capture image, then its not working. Image not store locally. I also want that user can capture only 5 images and when click on "send" button, That captured images and videos attached and emailed. Here is my code, What I done

        <!DOCTYPE html>
    <html>
      <head>
        <title>Capture Photo</title>

        <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
        <script type="text/javascript" charset="utf-8" src="emailcomposer.js"></script>


    <!-- ********************** -->

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


        //*********************************

                function deviceready() {
                    alert("Device ready");    
                     destinationType=navigator.camera.DestinationType;
                }

                 var destinationType; // sets the format of returned value


                function composeText(){
                    alert();
                    var file1 = document.getElementById('vehiclepic1').value;
                    //var message1 = document.getElementById('message_body').value;
                    alert(file1);
                    window.plugins.emailComposer.showEmailComposer(
                            "Get Estimation",
                            message1,
                            ["test@mail.com",],
                            [],
                            [],
                            true,
                            ["image.jpeg", "file.zip"]
                        );

                    alert('send');
                }

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

             function onPhotoDataSuccess(imageData) {
              // Uncomment to view the base64-encoded image data
              // alert(imageData);

              // Get image handle
              //
              var i = 0;
              if(imageData.length != 0){
                i++;
                //alert(i++);
              var smallImage = document.getElementById('vehiclepic1');

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

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

            function callAnotherPage(){
                window.location = "test.html";
            }


                document.addEventListener("deviceready", deviceready, true);


        //*********************************

        // Called when capture operation is finished
        //
        function captureSuccess(mediaFiles) {
        alert ("success");
        alert (mediaFiles.fullPath);
        //alert("path : "+mediaFiles.fullPath+", name : "+mediaFiles.name+", type : "+mediaFiles.type+", size : "+mediaFiles.size);

        //var i, path,len;
        //for (i = 0, len = mediaFiles.length; i < len; i += 1) {
         //    path = mediaFiles[i].fullPath;
        //}       
        }

        // Called if something bad happens.
        // 
        function captureError(error) {
        var msg = 'An error occurred during capture: ' + error.code;
        navigator.notification.alert(msg, null, 'Uh oh!');
        }

        // A button will call this function
        //
        function captureVideo() {
        // Launch device video recording application, 
        // allowing user to capture up to 2 video clips
        navigator.device.capture.captureVideo(captureSuccess, captureError, {limit: 2});
        }

        // Upload files to server
        function uploadFile(mediaFile) {
        var ft = new FileTransfer(),
            path = mediaFile.fullPath,
            name = mediaFile.name;

        ft.upload(path,
            "http://my.domain.com/upload.php",
            function(result) {
            alert('Upload success: ' + result.responseCode);
            alert(result.bytesSent + ' bytes sent');
            },
            function(error) {
            alert('Error uploading file ' + path + ': ' + error.code);
            },
            { fileName: name });   
        }
    </script>
    <!-- ********************** -->

    <script>
        var pictureSource;   // picture source
        var destinationType; // sets the format of returned value 

        // Wait for Cordova to connect with the device
        //
        document.addEventListener("deviceready",onDeviceReady,false);

        // Cordova is ready to be used!
        //
        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
          // alert(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;
        }

        // Called when a photo is successfully retrieved
        //
        function onPhotoURISuccess(imageURI) {
          // Uncomment to view the image file URI 
          // alert(imageURI);

          // Get image handle
          //
          var largeImage = document.getElementById('largeImage');

          // 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 capturePhoto() {
          // Take picture using device camera and retrieve image as base64-encoded string
          navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
        destinationType: destinationType.DATA_URL });
        }*/

        function capturePhoto() {
      // Take picture using device camera and retrieve image as base64-encoded string
          navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 25,
            destinationType: Camera.DestinationType.FILE_URI, });
            }

            function onPhotoDataSuccess(imageURI) {

              displayPhoto(imageURI);
            }


            function displayPhoto(imageURI)
            {

            capturedPhoto ++;

            if(capturedPhoto == 1){
            var part1 = document.getElementById('part1');
             part1.src =  imageURI;
            }
            else if(capturedPhoto == 2){
            var part2 = document.getElementById('part2');
              part2.src =  imageURI;
            }
            else if(capturedPhoto == 3){
            var part3 = document.getElementById('part3');
              part3.src =  imageURI;

            }
            else if(capturedPhoto == 4){
            var part4 = document.getElementById('part4');
              part4.src =  imageURI;

            }
            else if(capturedPhoto == 5){
            var part5 = document.getElementById('part5');
              part5.src =  imageURI;

            }
        }

        // 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>
      </head>
      <body>
        <button onclick="captureVideo();">Capture Video</button> <br>
        <button onclick="capturePhoto();">Capture Photo</button> <br>
        <!-- <button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br> -->
        <!-- <button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br> -->
        <button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
        <button onclick="composeText();">Send</button><br>
        <img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
        <img style="display:none;" id="largeImage" src="" />

        <u><b>Pictures</b></u>

        <ul>
        <li>
         <img style="width:100px;height:80px;" id="vehiclepic1" onclick="capturePhoto();" src="" />
        </li>


        </ul>



      </body>
    </html>

和这是code的工作相同的iOS设备或者我必须在改变什么?

And Is this code work same for Ios device or I have to change anything in that ?

我用EmailComposerWithAttachments从<插件href="https://github.com/NakedSwrod/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments"相对=nofollow> Github上

I use EmailComposerWithAttachments plugin from Github

推荐答案

如果您使用的是科尔多瓦3.0.0 ...您可以安装这个插件(的 https://github.com/steve-jansen/cordova-ios-emailcomposer )在本地使用下面的命令

if you are using Cordova 3.0.0 ... you can install this plugin(https://github.com/steve-jansen/cordova-ios-emailcomposer) locally using the following command

科尔多瓦插件添加emailcomposer#使用 http://plugins.cordova.io 注册表

cordova plugin add emailcomposer # using the http://plugins.cordova.io registry

 add the path of your image in the body as below code

var emailComposer = cordova.require('emailcomposer.EmailComposer');
emailComposer.show({ 
   to: toemail,
   subject: emailsubject,
   body: '<p> '+body+'</p><img alt="Embedded Image" height="200" width="200" src="'+path+'" />',
   isHtml: true,
});

图像路径是从这个函数

image path is taken from this function

 function sendEmail(imageData) {
   var largeImage = document.getElementById('largeImage');
       largeImage.style.display = 'block';
       largeImage.src = "data:image/jpeg;base64," + imageData;
  path = "data:image/jpeg;base64," + imageData; // this variable I used in body of email
  var cc = Ext.ComponentQuery.query('camerashare');
  cc[0].items.getByKey('ttbar').show();

}

这篇关于PhoneGap的:捕获图像,视频和发送附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆