views:

44

answers:

1

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  A: 

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)

Kevin Gaudin
You were right. Once I converted it to use a ContentProvider, GMail behaves a bit more sensibly. It's more code but it's probably better to do it this way than drop files in the Media store.
Dan Dyer