我必须用附件发送邮件的问题。 我使用JavaMail库(的mail.jar,activitation.jar和additional.jar)。 我可以准确地发送邮件。 但带有附件的图像邮寄我无法发送邮件。 我从图库中选择图片,并且它addded作为我的文件名
File f = new File("file://" + uri.getPath());
我想我已经当数据源接过我的文件的路径问题。 无论你可以看到我的代码更件事:(我已经解决了这个问题,这是我的代码的最后一种情况)
首先,我添加到我的查看附件:
Button Add = (Button) findViewById(R.id.btnAdd);
Add.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
onAddAttachment2("image/*");
}
});
这里是我的onAddAttachment2和onActivityResult代码
private void onAddAttachment2(final String mime_type) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(mime_type);
startActivityForResult(Intent.createChooser(i, null),
ACTIVITY_REQUEST_PICK_ATTACHMENT);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
mAttachments = (LinearLayout) findViewById(R.id.attachments);
switch (requestCode) {
case ACTIVITY_REQUEST_PICK_ATTACHMENT:
Uri _uri = imageReturnedIntent.getData();
addAttachment(_uri);
Cursor cursor = getContentResolver()
.query(_uri,
new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
String imageFilePath = cursor.getString(0);
uris.add(imageFilePath);
Log.v("imageFilePath", imageFilePath);
break;
}
}
正如你看到有我有一个AddAttachment方法。 下面是代码:
private void addAttachment(Uri uri) {
addAttachment(uri, null);
}
private void addAttachment(Uri uri, String contentType) {
long size = -1;
String name = null;
ContentResolver contentResolver = getContentResolver();
Cursor metadataCursor = contentResolver.query(uri, new String[] {
OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }, null,
null, null);
if (metadataCursor != null) {
try {
if (metadataCursor.moveToFirst()) {
name = metadataCursor.getString(0);
size = metadataCursor.getInt(1);
}
} finally {
metadataCursor.close();
}
}
if (name == null) {
name = uri.getLastPathSegment();
}
String usableContentType = contentType;
if ((usableContentType == null)
|| (usableContentType.indexOf('*') != -1)) {
usableContentType = contentResolver.getType(uri);
}
if (usableContentType == null) {
usableContentType = getMimeTypeByExtension(name);
}
if (size <= 0) {
String uriString = uri.toString();
if (uriString.startsWith("file://")) {
Log.v(LOG_TAG, uriString.substring("file://".length()));
File f = new File(uriString.substring("file://".length()));
size = f.length();
} else {
Log.v(LOG_TAG, "Not a file: " + uriString);
}
} else {
Log.v(LOG_TAG, "old attachment.size: " + size);
}
Log.v(LOG_TAG, "new attachment.size: " + size);
Attachment attachment = new Attachment();
attachment.uri = uri;
attachment.contentType = usableContentType;
attachment.name = name;
attachment.size = size;
View view = getLayoutInflater().inflate(
R.layout.message_compose_attachment, mAttachments, false);
TextView nameView = (TextView) view.findViewById(R.id.attachment_name);
ImageButton delete = (ImageButton) view
.findViewById(R.id.attachment_delete);
nameView.setText(attachment.name);
delete.setTag(view);
view.setTag(attachment);
mAttachments.addView(view);
delete.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
uris.remove(view.getTag());
mAttachments.removeView((View) view.getTag());
}
});
}
具有性能及附件类
static class Attachment implements Serializable {
private static final long serialVersionUID = 3642382876618963734L;
public String name;
public String contentType;
public long size;
public Uri uri;
}
终于在我的Mail.java类我有AddAttachment方法:
public void addAttachment(String file) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file);
_multipart.addBodyPart(messageBodyPart);
}
当我点击发送按钮 ,它已经发送给ADRESS被写入。 但我的附件不能正常显示。 我没有错误,当我发送的邮件。 我希望你有针对这些问题的解决方案...
编辑:OK最后我已经解决了这个问题。首先我定义! ArrayList<String> uris = new ArrayList<String>();
然后我在我这样的onActivityResult方法,用它uris.add(imageFilePath);
最后,前m.send
代码块,我添加图像:
for (int i = 0; i<uris.size(); i++)
{
m.addAttachment(uris.get(i).toString());
}
在我Mail.java类,显示这样的变化:
public void addAttachment(String file) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
FileDataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file);
_multipart.addBodyPart(messageBodyPart);
}