I am writing phone contacts to a file and exporting it through Email intent Action. Export works fine when i write the file to SD card.But when i write the file to phone memory of the emulator i get the mail without attachment. My Log displays "Unloaded attachment isn't marked for download".
Below is my code
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();
This is my Email Intent
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);
EDIT: The documnetation about unloaded attachment says
/**
* 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;
}
Which i understand is the path is the issue here.But the path is fyn when i printed it in log.
Any help is much appreciated. Thanks in advance.