Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中通过邮件附加文件_Android_Gmail_Attachment - Fatal编程技术网

如何在android中通过邮件附加文件

如何在android中通过邮件附加文件,android,gmail,attachment,Android,Gmail,Attachment,嗨,首先我想说的是一个使用gmail发送邮件的人在我的应用程序中点击一个按钮 现在,在上面的代码中,我无法附加文件。但后来我看到了Stack over flow的解决方案来解决这个问题,在GMailSender.java文件中做了一些修改,修改后的链接是 现在我的问题是我不理解修改后的部分。这是旧的部分 public synchronized void sendMailString主题、字符串正文、字符串发件人、字符串收件人 修改的部分是 public synchronized void sen

嗨,首先我想说的是一个使用gmail发送邮件的人在我的应用程序中点击一个按钮

现在,在上面的代码中,我无法附加文件。但后来我看到了Stack over flow的解决方案来解决这个问题,在GMailSender.java文件中做了一些修改,修改后的链接是

现在我的问题是我不理解修改后的部分。这是旧的部分

public synchronized void sendMailString主题、字符串正文、字符串发件人、字符串收件人

修改的部分是

public synchronized void sendMailString主题、字符串正文、字符串发件人、字符串收件人、文件附件

这不是一个重复的问题,我只是想知道这个文件附件部分是什么,我应该实现什么类型或方法来附加文件。如果您有任何疑问,请通过这两个链接找到解决方案,谢谢您提前传递一个名为附件的文件对象。这将是任何你想附加到你的电子邮件

您可以将其附加到电子邮件中,如:

MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);

MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment); //set attachment to filedatasource
mbp2.setDataHandler(new DataHandler(fds)); //add the filedatasource object to your 2nd mimebodypart
mbp2.setFileName(fds.getName());

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1); 
mp.addBodyPart(mbp2);

message.setContent(mp);
...send email...
编辑:从不需要从SD卡获取图像,但我认为您可以轻松创建一个文件对象,如下所示:

File imageFile = new File("path to image on sd card");
然后调用sendMail方法传入该文件对象

您正在传递一个名为附件的文件对象。这将是任何你想附加到你的电子邮件

您可以将其附加到电子邮件中,如:

MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);

MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment); //set attachment to filedatasource
mbp2.setDataHandler(new DataHandler(fds)); //add the filedatasource object to your 2nd mimebodypart
mbp2.setFileName(fds.getName());

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1); 
mp.addBodyPart(mbp2);

message.setContent(mp);
...send email...
编辑:从不需要从SD卡获取图像,但我认为您可以轻松创建一个文件对象,如下所示:

File imageFile = new File("path to image on sd card");
然后调用sendMail方法传入该文件对象

试试这个

Intent i = new Intent(Intent.ACTION_SEND);  
i.setType("message/rfc822") ; // use from live device
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});  
i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");  
i.putExtra(Intent.EXTRA_TEXT,"body goes here");  
i.putExtra(Intent.EXTRA_STREAM, new File(""));
startActivity(Intent.createChooser(i, "Select email application."));
试试这个

Intent i = new Intent(Intent.ACTION_SEND);  
i.setType("message/rfc822") ; // use from live device
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com"});  
i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");  
i.putExtra(Intent.EXTRA_TEXT,"body goes here");  
i.putExtra(Intent.EXTRA_STREAM, new File(""));
startActivity(Intent.createChooser(i, "Select email application."));

我不知道如何连接jpeg文件你从哪里提取jpeg?从我的计算机中提取的图片文件到我的模拟器上的虚拟存储卡这里有一个关于Android中数据存储的好教程,可以让你了解如何获取图像路径。我知道文件extStore=Environment.getExternalStorageDirectory;将为您提供SD卡的路径。File imageFile=new File/sdcard/ss.jpg;这是我们指定路径的方法再次感谢你我不明白如何附加jpeg文件你从哪里提取jpeg?图片文件从我的计算机到我的模拟器上的虚拟存储卡这里有一个关于Android中数据存储的好教程,可以让你了解如何获取指向您的图像的路径。我知道文件extStore=Environment.getExternalStorageDirectory;将为您提供SD卡的路径。File imageFile=new File/sdcard/ss.jpg;这是我们指明道路的方法谢谢你再次做得好