0

我正在尝试将我的 res/drawable 文件夹中的 png 文件作为电子邮件的附件发送。我可以将文件附加并包含在电子邮件中,但它缺少 .png 文件扩展名。我希望它在文件名中包含扩展名。有人可以帮忙吗?这是我的代码:

            Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
            i.putExtra(Intent.EXTRA_TEXT   , "Text");
            ArrayList<Uri> uris = new ArrayList<Uri>();
            Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.png_image);
            uris.add(newPath);

            i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(ShareActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
4

2 回答 2

0

将类型更改为:

i.setType("image/png");

虽然我相信putParcelableArrayListExtra()没有必要。putExtra()应该做的工作。

于 2012-08-27T05:49:15.187 回答
0

使用此方法获取文件的扩展名

public static String getMimeType(Context context, Uri uri) {
    String extension;

    //Check uri format to avoid null
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        //If scheme is a content
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
    } else {
        //If scheme is a File
        //This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
        extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());

    }

    return extension;
}
于 2016-08-25T09:20:05.330 回答