[英]How to pass a camera image to mail as an attachment. (Android, iOS, Windows Phone, Phonegap)
我正在為自己的工作開發一個應用程序(社會住房),我希望它能夠允許用戶拍攝照片並將其附加到電子郵件中,以便他們可以將其發送給我們(維修圖片等)
我正在使用Phonegap和Eclipse,因為我希望該應用程序可以跨平台運行,但目前主要在Android中進行測試。 有沒有辦法做到這一點? 我目前正在使用下面的代碼,但無濟於事。
<script typr="text/javascript" charset="utf-8">
function camera()
{
navigator.camera.getPicture(onSuccess, onFail, { quality: 20,
destinationType: Camera.DestinationType.DATA_URL
});
function onSuccess(imageData) {
var image = document.getElementById('image');
var data = "data:image/jpeg;base64," + imageData;
var link = "mailto:johnsmith@gmail.com?body="+data+"&subject=john smith";
window.location.href = link;
}
function onFail(message) {
alert('Failed because: ' + message);
}
}
</script>
到目前為止,我已經嘗試使用mailto:&attachment方法將數據傳遞到郵件應用程序,但是它從未附加圖像(大多數郵件應用程序將此視為安全漏洞)。 然后,我嘗試將圖像的base64代碼嵌入到電子郵件正文中(如上所示)。 不幸的是,base64僅顯示為純文本,並使郵件無響應。 我也曾嘗試在Phonegap中使用圖像URI而不是Base64方法,但是這在我的logcat中引發了“未定義image.URI”錯誤。
這可能嗎? 我知道我可以在Android上使用Intent,如此處另一個問題所述,但這在iOS等系統上不起作用。
任何幫助將不勝感激。
編輯02/12/2012
我要在這里實現的功能與您在本機Android gallery / camera app中獲得的功能相同。 拍照后,您可以使用共享選項,其中之一就是郵件。 如果您選擇通過郵件共享,則圖像將作為附件傳遞到郵件應用程序。 有什么辦法可以在我的應用程序中實現相同的功能?
因此,似乎沒有“一刀切”的解決方案。
mailto:方法只是在大多數現代郵件應用程序中不傳遞附件,因為它被視為存在安全風險。 因此,無論是imageURI還是base64編碼圖像,mailto:都將無法工作。 不過,對於希望使用以上代碼預先填寫無附件的電子郵件的人來說,傳遞“主題”和“正文”效果很好。
在其他地方提出此問題后,我似乎需要使用phonegap插件(適用於iOS的emailComposer和適用於Android的WebIntent)才能將圖像成功地從我的phonegap應用傳遞到郵件應用。
謝謝。
使用此JAVA代碼發送帶有照片和文字的電子郵件。
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
} catch (Exception e) {
}
}
public synchronized void sendMail(String subject, String body, String senderEmail, String recipients, String filePath,String logFilePath) throws Exception {
boolean fileExists = new File(filePath).exists();
if (fileExists) {
String from = senderEmail;
String to = recipients;
String fileAttachment = filePath;
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// create the message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
// fill message
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("screenShoot.jpg");
multipart.addBodyPart(messageBodyPart);
//part three for logs
messageBodyPart = new MimeBodyPart();
DataSource sourceb = new FileDataSource(logFilePath);
messageBodyPart.setDataHandler(new DataHandler(sourceb));
messageBodyPart.setFileName("logs.txt");
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send(message);
}else{
sendMail( subject, body, senderEmail, recipients);
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.