问题描述:

I'm currently learning to develop Android apps and have a fairly basic question:

Suppose I want to send out an implicit Intent to open up data specified by a URI link, e.g. "content://com.mynewauthority:8100/table1". And suppose I have created a custom Content Provider that maintains an SQLite table at "content://com.mynewauthority:8100/table1".

Now since I haven't specified the MIME type for my table, but only the URI to it, Android must be able to figure it out somehow. But since it is a custom-made table, I decide on the MIME type myself, so it could be "monkey/bananas" for all I know. I can't seem to find anywhere in the Content Provider implementation that specifies which MIME type goes with which URIs except the getType(URI uri) function which doesn't seem to be used by the Android system for Intent resolution.

So my question is: How does Android figure out the MIME types associated with URIs for custom-made content providers during resolution of implicit Intents?

Hope this question is clear enough!

网友答案:

If the Intent has a MIME type in it (see XGouchet's answer), then that is used.

If the Intent does not have a MIME type in it, Android will attempt to determine a MIME type:

  • For a ContentProvider, as in your case, it calls getType() on the specific provider for that Uri, based on the authority
  • For an http:// Uri, it issues an HTTP HEAD request
  • For most other stuff, it fails (e.g., a file:// Uri)
网友答案:

If you create your intent object yourself, you can force the mime type attached to it by using :

fileType = "monkey/bananas";
intent.setDataAndType(fileUri, fileType);

FYI, android usually detects the mime types from the file extension, using the MimeTypeMap, which I find rather incomplete. You can print the known file extensions and associated mime types from the MimeTypeMap class source code

相关阅读:
Top