How to specify filename for ACTION_SEND?

2019-02-26 03:57发布

问题:

I'm using ACTION_SEND to send a file from the content provider in my app as an email attachment in GMail. This works great, except that I can't programmatically specify the filename of the attachment. When my URI is

content://my.documentcontentprovider/321

The filename of the attachment is 321. 321 is the document id.

I looked at the spec for Intent, and don't see an EXTRA_ key for specifying the filename. The only workaround I could come up with is to append the filename to my URI:

content://my.documentcontentprovider/321/photo.jpg

It should work, but it seems a little hacky. If someone has a better idea, please chime in. Here's my code, in case it's useful:

    Intent sendDoc = new Intent();
    sendDoc.setAction(Intent.ACTION_SEND);
    sendDoc.putExtra(Intent.EXTRA_SUBJECT, doc.name);
    sendDoc.putExtra(Intent.EXTRA_STREAM, DocumentContentProvider.getUri(doc.id));
    sendDoc.setType(doc.contentType);
    this.foldersFragment.startActivity(sendDoc);

回答1:

You'll need to pass the filepath as Intent.EXTRA_STREAM. Well-designed ContentProviders expose filepaths in the _DATA columns.