0

我正在将电话联系人写入文件并通过电子邮件意图操作将其导出。当我将文件写入 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 的。

任何帮助深表感谢。提前致谢。

4

3 回答 3

0

我发现本机电子邮件应用程序接受您需要调用的附件Intent.setType以及使用而ACTION_SEND不是ACTION_SENDTO.

以下是我最终构建让电子邮件附件在 gmail 和本机应用程序中工作的意图的方式:

Intent intent  = new Intent(Intent.ACTION_SEND); // SENDTO does not work for native email app
        intent.setData(Uri.fromParts("mailto",to, null));
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, body);
        intent.setType("message/email");
        if (attachmentInCacheDir != null) {
            intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + getAuthority(context) + "/" + attachmentInCacheDir.getName()));
        }
        context.startActivityForResult(Intent.createChooser(intent, "Send Email"), 1000);

如您所见,我还使用内容提供程序进行文件访问,以避免将文件复制到外部存储。这是一个全新的蠕虫罐,因为本机应用程序在您的提供程序上调用“查询”以询问文件大小。如果大小为零,则不会附加。Gmail 要宽松得多。

于 2015-03-11T23:59:05.683 回答
0

根据我最近的经验,将 Intent.EXTRA_STREAM 与外部文件 Uri 一起使用——无论它位于 getExternalCacheDir ()、getExternalFilesDir () 还是 Environment.getExternalStorageDirectory ()——将有效地与许多电子邮件客户端应用程序一起使用(可能:我只测试了 GMail、Evernote 和本机电子邮件客户端)...但值得注意的是:通过本机电子邮件应用程序除外...

我昨天所做的测试涉及针对 MS Exchange 公司电子邮件帐户的本机电子邮件应用程序,并且我收到了“未加载的附件未标记为下载:”调试消息。电子邮件最初看起来制作正确,带有附件(即使显示了附件文件大小!),但发送的电子邮件没有附件。

使用 GMail 客户端没有这样的行为:一切都很好。我想在发送之前在 GMail 中查看附件文件的大小,但文件是与电子邮件一起发送的。在它的一边,Evernote 创建一个带有附件文件的可编辑笔记作为嵌入对象。

当我查看您发布的参考源代码时,我认为 Android 原生电子邮件应用程序开发人员对附件文件做出了一些不正确的假设。他们根本没有提供来自 Intent.EXTRA_STREAM 的附件...这很奇怪,因为附件在发送前与消息一起显示,因此它可能根本不会在本机电子邮件应用程序数据库中创建电子邮件时保存,或者附件在过程中被错误地标记/取消标记。

我知道这本身并不能回答问题,但至于通过本机电子邮件应用程序发送附件(我可以在 Android ICS 中说这么多),我想我们被卡住了!

于 2012-07-20T15:27:15.677 回答
0

这是文档中有关内部存储的内容:

默认情况下,保存到内部存储的文件对您的应用程序是私有的,其他应用程序无法访问它们(用户也不能)。

所以我现在正在尝试在我的应用程序中使用本机 android 电子邮件应用程序附加文件的功能。

或者我可以使用其他后端开发技术来附加包含我要导出的内容的文件。

于 2012-07-20T06:28:08.343 回答