【问题标题】:How to attach a image and a .txt(multiple files) file to gmail through Coding?如何通过编码将图像和 .txt(多个文件)文件附加到 gmail?
【发布时间】:2012-09-14 10:43:37
【问题描述】:

我正在尝试附加一个图像文件和 .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 文件,那么它就是附加的。我想附上这两个文件请帮忙。

【问题讨论】:

  • 如果我使用 ACTION_SEND_MULTIPLE 则应用程序会强制关闭。向我们展示 logcat。您必须使用 ACTION_SEND_MULTIPLE 来附加多个文件。

标签: android gmail uri email-attachments


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 2015-11-25
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多