Search code examples
androidshareintentfilter

How to share anything?


As a learning exercise, I'm writing an app that has a android.intent.action.SEND intent. I want to be able to have it send anything I can come up with a filename for, but all the examples I see of defining such apps are specific to images in the mediastore, ringtones in the ringtone manager, etc. I don't want to enumerate every possible algorithm I've found by google searches for converting a Uri to a file. I'd like a generic solution. I've seen an OPENABLE category which sounds promising, but I still haven't seen a generic solution for discovery of the underlying filename. Is there one?

I just want this thing to upload whatever content it is handed to a php script that implements a POST method for doing file uploads. I don't see any reason to restrict it to images or video, or something like that, but for the life of me I can't figure out how to make it work for any random file some other app wants to share.


Solution

  • I'm looking for a file name to use

    There is no guarantee that there is a file name to use. Particularly if the scheme is content://, it is quite likely that there is no file name, because in some cases there is no file (e.g., the data is a BLOB in a database).

    Off the cuff, I'd use an algorithm like this: if the scheme is content:// and getLastPathSegment() on the Uri returns an integer, generate a filename; otherwise, use getLastPathSegment(). That should cover content://, file://, and http:// schemes reasonably well, and I can't think of any other scheme you're likely to encounter in an ACTION_SEND-handling activity.