我正在开发一个Android项目,我从相机点击图片或从图库中选择,我需要将该图像作为邮件中的附件发送。这是我从相机enter code here
public void launchCamera(View view){
gallery.setEnabled(false);
String fileName = "abc.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION," XYZ");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
以下是从图库中获取图片的代码
public void launchGallery(View view){
cameraButton.setEnabled(false);
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent,PICK_IMAGE_REQUEST);
这是onActivityResult函数
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
String imageId = convertImageUriToFile( imageUri,CameraActivity);
}
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filep = cursor.getString(columnIndex);
cursor.close();
}
}
public String convertImageUriToFile ( Uri imageUri, Activity activity ) {
Cursor cursor = null;
int imageID = 0;
try {
/*********** Which columns values want to get *******/
String[] proj = {
MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Thumbnails._ID,
MediaStore.Images.ImageColumns.ORIENTATION
};
cursor = getContentResolver().query(imageUri, proj, null, null, null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int columnIndexThumb = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int thumbID = 0;
if (cursor.moveToFirst()) {
imageID = cursor.getInt(columnIndex);
thumbID = cursor.getInt(columnIndexThumb);
filep = cursor.getString(file_ColumnIndex);
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return filep;
}
发送电子邮件的代码是
public class SendMail
{
private Context context;
private ProgressDialog progressDialog;
public void sending(String filepath, String rec, String bod)
{
// Recipient's email ID needs to be mentioned.
String to = rec;
// Sender's email ID needs to be mentioned
final String from = "abc@gmail.com";
// final String username = "xyz";
final String pass = "qwerty";
// Assuming you are sending email from localhost
String host = "smtp.gmail.com";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", pass);
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Plant Trees");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(bod);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filepath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filepath);
multipart.addBodyPart(messageBodyPart);
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822");
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
在主要活动中调用发送功能的代码是
public void sem()
{
SendMail sm=new SendMail();
sm.sending(filep,"abc@gmail.com","xyz");
}
点击发送按钮后,当我运行时,应用程序崩溃了 请帮助也抱歉错误和大代码大小,但我刚刚学习Android,这是我的第一个项目。在此先感谢