我在我的应用中使用电子邮件编辑器。我从Android下载插件。 https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments。 之后我会做同样的事情,写在自述文件中。
在我的src代码中添加java文件。
在我的js文件夹中添加js文件。
在config.xml
文件中添加插件。
<plugin name="EmailComposer" value="com.example.procat"/>
但我仍然收到此错误。
***06-30 11:23:47.507: D/CordovaLog(660): ****************************AVVIATO
06-30 11:23:47.507: I/Web Console(660): ****************************AVVIATO at file:///android_asset/www/js/EmailComposer.js:20
06-30 11:23:47.507: W/System.err(660): java.lang.ClassNotFoundException: com.example.procat
06-30 11:23:47.517: W/System.err(660): at java.lang.Class.classForName(Native Method)
06-30 11:23:47.517: W/System.err(660): at java.lang.Class.forName(Class.java:227)
06-30 11:23:47.517: W/System.err(660): at java.lang.Class.forName(Class.java:174)
06-30 11:23:47.517: W/System.err(660): at org.apache.cordova.api.PluginEntry.getClassByName(PluginEntry.java:102)
06-30 11:23:47.517: W/System.err(660): at org.apache.cordova.api.PluginEntry.createPlugin(PluginEntry.java:78)
06-30 11:23:47.517: W/System.err(660): at org.apache.cordova.api.PluginManager.getPlugin(PluginManager.java:258)
06-30 11:23:47.517: W/System.err(660): at org.apache.cordova.api.PluginManager.exec(PluginManager.java:215)
06-30 11:23:47.527: W/System.err(660): at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:51)
06-30 11:23:47.527: W/System.err(660): at org.apache.cordova.CordovaChromeClient.onJsPrompt(CordovaChromeClient.java:221)
06-30 11:23:47.527: W/System.err(660): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:581)
06-30 11:23:47.527: W/System.err(660): at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 11:23:47.537: W/System.err(660): at android.os.Looper.loop(Looper.java:132)
06-30 11:23:47.537: W/System.err(660): at android.app.ActivityThread.main(ActivityThread.java:4123)
06-30 11:23:47.537: W/System.err(660): at java.lang.reflect.Method.invokeNative(Native Method)
06-30 11:23:47.537: W/System.err(660): at java.lang.reflect.Method.invoke(Method.java:491)
06-30 11:23:47.537: W/System.err(660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-30 11:23:47.537: W/System.err(660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-30 11:23:47.537: W/System.err(660): at dalvik.system.NativeStart.main(Native Method)
06-30 11:23:47.547: W/System.err(660): Caused by: java.lang.NoClassDefFoundError: com.example.procat
06-30 11:23:47.547: W/System.err(660): ... 18 more
06-30 11:23:47.557: W/System.err(660): Caused by: java.lang.ClassNotFoundException: com.example.procat in loader dalvik.system.PathClassLoader[/data/app/com.example.procat-1.apk]
06-30 11:23:47.557: W/System.err(660): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
06-30 11:23:47.568: W/System.err(660): at java.lang.ClassLoader.loadClass(ClassLoader.java:540)
06-30 11:23:47.568: W/System.err(660): at java.lang.ClassLoader.loadClass(ClassLoader.java:500)
06-30 11:23:47.568: W/System.err(660): ... 18 more
06-30 11:23:47.568: I/System.out(660): Error adding plugin com.example.procat.
06-30 11:23:47.568: D/PluginManager(660): exec() call to unknown plugin: EmailComposer***
答案 0 :(得分:2)
插件需要更新更新版本的Cordova。您可以下载包含更新的插件代码和生成的APK from here的Eclipse测试项目。
请注意,由于Android 4.x中的this bug,我必须在EmailComposer.java中替换此行:
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
与
ArrayList<String> extra_text = new ArrayList<String>();
extra_text.add(body);
emailIntent.putStringArrayListExtra(android.content.Intent.EXTRA_TEXT, extra_text);
这解决了纯文本电子邮件的问题,但不适用于HTML电子邮件,因为Spanned(由Html.fromHtml返回)不是Charsequence的子类。当我尝试将Html.fromHtml()的结果转换为字符串时,标记显示为文本的一部分: - (
此外,当我尝试此操作时,使用Gmail应用程序时会显示纯文本电子邮件,但它没有显示在库存中。电子邮件应用程序 - 正文始终为空白。
更新插件的代码:
<强> EmailComposer.java 强>
/**
*
* Phonegap Email composer plugin for Android with multiple attachments handling
*
* Version 1.0
*
* Guido Sabatini 2012
*
*/
package org.apache.cordova.plugin;
import java.io.File;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.LOG;
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
}
}
<强> 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;
});
将config.xml中的行更改为:
<plugin name="EmailComposer" value="org.apache.cordova.plugin.EmailComposer"/>
然后对index.html这样的事情:
<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova-2.8.0.js"></script>
<script type="text/javascript" charset="utf-8" src="emailcomposer.js"></script>
<script type="text/javascript">
function deviceready() {
console.log("Device ready");
}
function composeHTML(){
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 <b>this<b/>:",
["fred@blogs.com", "peter@pan.com"],
[],
[],
true,
["image.jpg", "file.zip"]
);
}
function composeText(){
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:",
["fred@blogs.com", "peter@pan.com"],
[],
[],
false,
["image.jpg", "file.zip"]
);
}
document.addEventListener("deviceready", deviceready, true);
</script>
</head>
<body>
<h1>EmailComposer</h1>
<button onclick="composeText();">Compose Text Email</button>
<button onclick="composeHTML();">Compose HTML Email</button>
</body>
</html>