我正在将电话联系人写入文件并通过电子邮件意图操作将其导出。当我将文件写入 SD 卡时,导出工作正常。但是当我将文件写入模拟器的手机内存时,我收到的邮件没有附件。我的日志显示“未加载的附件未标记为下载”。
下面是我的代码
file = new File(ctx.getFilesDir().getAbsolutePath(),"file.txt");
if (file.exists()) {
file.delete();
}
writer = new BufferedWriter(new FileWriter(file));
for (int i = 0; i < names.size(); i++) {
writer.write(names.get(i) + "," + phnno.get(i) + "\r\n");
}
writer.flush();
writer.close();
这是我的电子邮件意图
Uri u1 = null;
u1 = Uri.fromFile(file);
System.out.println("u1 of URI"+u1); //u1 in logcat is "file:///data/data/com.android.contactxport/files/file.txt"
Intent sendIntent = new Intent(
Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT,
"Mail from ContactXPort App");
sendIntent
.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/html");
startActivity(sendIntent);
编辑:关于卸载附件的文档说
/**
* Check whether the message with a given id has unloaded attachments. If the message is
* a forwarded message, we look instead at the messages's source for the attachments. If the
* message or forward source can't be found, we return false
* @param context the caller's context
* @param messageId the id of the message
* @return whether or not the message has unloaded attachments
*/
public static boolean hasUnloadedAttachments(Context context, long messageId) {
Message msg = Message.restoreMessageWithId(context, messageId);
if (msg == null) return false;
Attachment[] atts = Attachment.restoreAttachmentsWithMessageId(context, messageId);
for (Attachment att: atts) {
if (!attachmentExists(context, att)) {
// If the attachment doesn't exist and isn't marked for download, we're in trouble
// since the outbound message will be stuck indefinitely in the Outbox. Instead,
// we'll just delete the attachment and continue; this is far better than the
// alternative. In theory, this situation shouldn't be possible.
if ((att.mFlags & (Attachment.FLAG_DOWNLOAD_FORWARD |
Attachment.FLAG_DOWNLOAD_USER_REQUEST)) == 0) {
Log.d(Logging.LOG_TAG, "Unloaded attachment isn't marked for download: " +
att.mFileName + ", #" + att.mId);
Attachment.delete(context, Attachment.CONTENT_URI, att.mId);
} else if (att.mContentUri != null) {
// In this case, the attachment file is gone from the cache; let's clear the
// contentUri; this should be a very unusual case
ContentValues cv = new ContentValues();
cv.putNull(AttachmentColumns.CONTENT_URI);
Attachment.update(context, Attachment.CONTENT_URI, att.mId, cv);
}
return true;
}
}
return false;
}
我的理解是路径是这里的问题。但是当我在日志中打印它时,路径是 fyn 的。
任何帮助深表感谢。提前致谢。