android - 如何通过编码将图像和.txt(多个文件)文件附加到gmail?

标签 android gmail uri email-attachments

我正在尝试附加图像文件和 .txt 文件,但只有一个文件正在附加,一个没有附加如何附加这两个文件。 如果我使用 ACTION_SEND_MULTIPLE,则应用会强制关闭。

这是我的代码:

public static Intent getSendEmailIntent(Context context, String email,
        String subject, String body, String fileName) {


    final Intent emailIntent = new Intent(Intent.ACTION_SEND);

    // Explicitly only use Gmail to send
    emailIntent.setClassName("com.google.android.gm",
            "com.google.android.gm.ComposeActivityGmail");

    emailIntent.setType("*/*");

    // Add the recipients
    emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
            new String[] { email }
            );

    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);

    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

    // Add the attachment by specifying a reference to our custom
    // ContentProvider
    // and the specific file of interest
    emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.txt"));
    emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/"+ "anything.jpg"));

    return emailIntent;
}

如果我在返回之前最后放了一个 .jpg 文件,它是附加的。但是,如果我最后放了一个 .txt 文件,那么它就是附加的。我想附上这两个文件,请帮忙。

最佳答案

您可以只使用下面的代码并将文件路径更改为您的文件。

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/IMG.jpg"))); // Add your image URIs here
imageUris.add(Uri.fromFile(new File("/mnt/sdcard/Medplann/Report.pdf")));

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));

阅读此页:send

关于android - 如何通过编码将图像和.txt(多个文件)文件附加到gmail?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12422910/

相关文章:

ruby - 在 Ruby 中提取 URI 最后一段的最佳方法

android - ActionBar 兼容 ActionBarActivity setTitle 在 Android 2.X 中不起作用

android - 在 React Native 的胜利图表中找不到方法 google() 错误

android - android中如何在xml解析中解析数据并在listview中显示数据

java - JSONObject 返回类型

java - 使用 URI userinfo 组件进行身份验证 http ://userinfo@hostname:port/path in a servlet

oauth - 通过 GMail XOAUTH 出现 IMAP "Invalid Credentials"错误

email - 您可以将data:uri格式的图像发送到GMail吗?

javascript - GMail "Loading..."横幅是如何工作的?

.net - 从基础创建 Uri 没有尾部斜杠和相关部分