Sending PNG attachment via Android GMail app

陌路散爱 提交于 2019-12-08 08:27:08

问题


I'm attaching a PNG image to an e-mail with the following code:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, title);
values.put(MediaStore.Images.Media.DESCRIPTION, title);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
Uri uri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
OutputStream stream = activity.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
stream.close();

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_SUBJECT, "Message title");
intent.putExtra(Intent.EXTRA_TEXT, "Message body");
intent.putExtra(Intent.EXTRA_STREAM, uri);
activity.startActivity(Intent.createChooser(intent, activity.getString(R.string.share)));

The native HTC e-mail app copes with this fine but the GMail app insists on giving the attachment a ".jpg" extension even though the image is definitely a PNG. This may just be a bug in the GMail app but I thought I'd ask anyway.

This is what logcat shows when the attachment is attached:

I/Gmail   (  285): >>>>> Attachment uri: content://media/external/images/media/11
I/Gmail   (  285): >>>>>           type: image/png
I/Gmail   (  285): >>>>>           name: 1287752711300.jpg
I/Gmail   (  285): >>>>>           size: 0

Is there some way to control the file name used by the GMail app? I couldn't find anything obvious in the Intent extras documentation. Ideally I would be able to specify the full name, but even just getting it to use the right file extension would be a victory.


回答1:


Maybe the GMail app assumes that content delivered by the media store app are always jpg pictures. It might even be the Media store which could provide wrong data to the GMail app.

You could try to store your PNG file on the file system and as the GMail doesn't fully support file:// Uris (it works only with files on external storage) you may use a custom ContentProvider to simply provide content from a file with a content:// uri instead. You can find such a content provider here.

One other simple test could be to send a PNG file from the filesystem through GMail using a File explorer app like EStrongs or Astro. If GMail still forces the .jpg extension then you may have hard time finding a way to work this around.

Last idea, try using a more generic mime-type like application/octet-stream (but this could lead to problems for in mail receivers mail applications which might not understand what kind of file it is)



来源:https://stackoverflow.com/questions/3997229/sending-png-attachment-via-android-gmail-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!