如何在 Phonegap 中安装插件

Posted

技术标签:

【中文标题】如何在 Phonegap 中安装插件【英文标题】:How to Install plug-in in Phonegap 【发布时间】:2013-11-15 05:44:41 【问题描述】:

我是 phone-gap 的新手,正在开发一款适用于 androidios 的应用程序。 现在,我的要求是我想发送带有附件的电子邮件,所以我在互联网上搜索并找到了这个插件,EmailComposer-phonegap-plugin。他们在下面写了 4 点。

    将 EmailComposer.h EmailComposer.m 文件添加到您的插件文件夹。 将 EmailComposer.js 文件放在 www 文件夹中的某个位置,并从 html 中包含它。

    添加到插件下的config.xml:或者,如果使用新格式,

    <feature name="EmailComposer"> <param name="ios-package" value="EmailComposer" /> </feature>
    

    在 android 上,在 value 属性中一定要包含包名,例如

    value="org.apache.cordova.EmailComposer"
    

现在我的问题是如何使用这个插件? 我完成了第 2 点,但不知道将文件作为第 1 点放置在哪里?因为我不知道我的“插件”文件夹在哪里。以及如何处理第 3 点和第 4 点?

我也在互联网上找到此代码。但“附加文件”警报不起作用。

var smallImage = document.getElementById('smallImage').value
alert("Send Mail");
window.plugins.emailComposer.showEmailComposer(
        "Send Images",
        smallImage,
        ["test@email.com",],
        [],
        [],
        true,
        ["image.jpeg", "file.zip"],
        alert("Attach file");
    );
    alert("Send Mail Success");

那么,我必须做些什么才能成功发送附件邮件?

【问题讨论】:

已经安装了cord-ova插件... @ManetiVinay 我添加了科尔多瓦的 jar 文件并将其添加到构建路径并添加其 js 文件 你想要的是在android和ios中放置插件的位置.. @ManetiVinay 是的。在项目的哪个文件夹中? 【参考方案1】:

试试这个它对我的工作......

index.html

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <meta name="format-detection" content="telephone=no" />
            <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

            <title>Hello World</title>
        </head>
        <body>

            <script type="text/javascript" src="phonegap.js"></script>
            <script type="text/javascript" src="js/EmailComposer.js"></script>  

             <script type="text/javascript">            
             document.addEventListener("deviceready",onDeviceReady,false);
                function onDeviceReady() 


                            cordova.require('cordova/plugin/emailcomposer').showEmailComposer(
                                function()  console.log( 'successfully called email composer' ); ,
                                function()  console.log( 'failed to call email composer' ); ,
                                "Look at this photo",
                                "Take a look at this:",
                                 ["amitd.prajapati@test.com"],
                                 [],
                                 [],
                                false,
                                ["/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png","/mnt/sdcard/amit/amit.png"]
                            );
                        

             </script>
        </body>
    </html>

EmailComposer.js

    cordova.define("cordova/plugin/emailcomposer", function(require, exports, module) 
        var exec = require('cordova/exec');

        var EmailComposer = function() 
            this.resultCallback = null; // Function
        ;

        EmailComposer.ComposeResultType = 
            Cancelled:0,
            Saved:1,
            Sent:2,
            Failed:3,
            NotSent:4
        


        // showEmailComposer : all args optional
        EmailComposer.prototype.showEmailComposer = function(successCallback,failureCallback,subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML,attachments) 
        console.log("showEmailComposer()");
        var args = ;
        if(toRecipients)
            args.toRecipients = toRecipients;
        if(ccRecipients)
            args.ccRecipients = ccRecipients;
        if(bccRecipients)
            args.bccRecipients = bccRecipients;
        if(subject)
            args.subject = subject;
        if(body)
            args.body = body;
        if(bIsHTML)
            args.bIsHTML = bIsHTML;
        if(attachments)
            args.attachments = attachments;

        cordova.exec(successCallback, failureCallback, "EmailComposer", "showEmailComposer", [args]);
    


        var emailcomposer = new EmailComposer();
        module.exports = emailcomposer;
    );

EmailComposer.java

    package com.inic.emailattacher.emailcomposser;

    import java.io.File;
    import java.util.ArrayList;

    import org.apache.cordova.CallbackContext;
    import org.apache.cordova.CordovaPlugin;
    import org.apache.cordova.LOG;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.content.Intent;
    import android.net.Uri;
    import android.text.Html;


    public class EmailComposer extends CordovaPlugin 

       @Override
       public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
           if ("showEmailComposer".equals(action)) 

               try 
                   JSONObject parameters = args.getJSONObject(0);
                   if (parameters != null) 
                       sendEmail(parameters);
                   
                catch (Exception e) 

               
               callbackContext.success();
               return true;
           
           return false;  // Returning false results in a "MethodNotFound" error.
       

       private void sendEmail(JSONObject parameters) 

           final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

           //String callback = parameters.getString("callback");

           boolean isHTML = false;
           try 
               isHTML = parameters.getBoolean("bIsHTML");          
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling isHTML param: " + e.toString());
           

           if (isHTML) 
               emailIntent.setType("text/html");
            else 
               emailIntent.setType("text/plain");
           

           // setting subject
           try 
               String subject = parameters.getString("subject");
               if (subject != null && subject.length() > 0) 
                   emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
               
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling subject param: " + e.toString());
           

           // setting body
           try 
               String body = parameters.getString("body");
               if (body != null && body.length() > 0) 
                   if (isHTML)                                        
                       String bodyHtml = Html.fromHtml(body).toString();
                       LOG.e("EmailComposer", "Creating HTML email with body: " + bodyHtml);
                       ArrayList<String> extra_text = new ArrayList<String>();
                       extra_text.add(bodyHtml);
                       emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
                    else 
                       LOG.e("EmailComposer", "Creating text email with body: " + body);
                       ArrayList<String> extra_text = new ArrayList<String>();
                       extra_text.add(body);
                       emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
                   
               
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling body param: " + e.toString());
           

           // setting TO recipients
           try 
               JSONArray toRecipients = parameters.getJSONArray("toRecipients");
               if (toRecipients != null && toRecipients.length() > 0) 
                   String[] to = new String[toRecipients.length()];
                   for (int i=0; i<toRecipients.length(); i++) 
                       to[i] = toRecipients.getString(i);
                   
                   emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to);
               
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling toRecipients param: " + e.toString());
           

           // setting CC recipients
           try 
               JSONArray ccRecipients = parameters.getJSONArray("ccRecipients");
               if (ccRecipients != null && ccRecipients.length() > 0) 
                   String[] cc = new String[ccRecipients.length()];
                   for (int i=0; i<ccRecipients.length(); i++) 
                       cc[i] = ccRecipients.getString(i);
                   
                   emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc);
               
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling ccRecipients param: " + e.toString());
           

           // setting BCC recipients
           try 
               JSONArray bccRecipients = parameters.getJSONArray("bccRecipients");
               if (bccRecipients != null && bccRecipients.length() > 0) 
                   String[] bcc = new String[bccRecipients.length()];
                   for (int i=0; i<bccRecipients.length(); i++) 
                       bcc[i] = bccRecipients.getString(i);
                   
                   emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc);
               
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling bccRecipients param: " + e.toString());
           

           // setting attachments
           try 
               JSONArray attachments = parameters.getJSONArray("attachments");
               if (attachments != null && attachments.length() > 0) 
                   ArrayList<Uri> uris = new ArrayList<Uri>();
                   //convert from paths to Android friendly Parcelable Uri's
                   for (int i=0; i<attachments.length(); i++) 
                       try 
                           File file = new File(attachments.getString(i));
                           if (file.exists()) 
                               Uri uri = Uri.fromFile(file);
                               uris.add(uri);
                           
                        catch (Exception e) 
                           LOG.e("EmailComposer", "Error adding an attachment: " + e.toString());
                       
                   
                   if (uris.size() > 0) 
                       emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                   
               
            catch (Exception e) 
               LOG.e("EmailComposer", "Error handling attachments param: " + e.toString());
           

           this.cordova.startActivityForResult(this, emailIntent, 0);
       

       @Override
       public void onActivityResult(int requestCode, int resultCode, Intent intent) 
           // TODO handle callback
           super.onActivityResult(requestCode, resultCode, intent);
           LOG.e("EmailComposer", "ResultCode: " + resultCode);
           // IT DOESN'T SEEM TO HANDLE RESULT CODES
       

    

res->xml->config.xml

 <feature name="EmailComposer">
    <param name="android-package" value="com.inic.emailattacher.emailcomposser.EmailComposer" />
</feature>

AndroidManifest.xml

     <uses-permission android:name="android.permission.INTERNET" />

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

【讨论】:

嗯,这听起来不错。让我试试看。还有一个问题。我捕获快照并显示在此 ID“smallImage”上,如果我想将其附加到我的电子邮件中该怎么办? 我在哪里放置“EmailComposer.java”文件? 创建新包,就像我创建“com.inic.emailattacher.emailcomposser”一样,我有 EmailComposer.java 正如我上面写的,我是新手。如果你技术我如何创建新的包和使用,这对我有好处。你也可以在Skype上加我 显示错误“无法解析导入 org.apache.cordova.CallbackContext”、“无法解析导入 org.apache.cordova.CordovaPlugin”和“导入 org.apache.cordova .LOG 无法解析”在导入部分

以上是关于如何在 Phonegap 中安装插件的主要内容,如果未能解决你的问题,请参考以下文章

如何在myeclipse中安装spring插件

如何在eclipse中安装testng插件

如何在eclipse中安装插件

如何在本地安装 Kong 中安装自定义插件

如何在pycharm中安装python的插件

怎样在Eclipse中安装Flex Builder插件 详细一点 谢谢