I really can’t get the meaning of mimeType. I know that it exists so that the getType
method in ContentProvider
knows what to match with it. But I’m still not sure what it means or how it’s used.
Any ContentProvider
usually defines the type of data it handles (e.g. NotePadProvider handles a Notes
data type defined in an inner class of NotePad). A MIME type is just a standardized way to define that data type by giving it a unique name. This allows the data type to be communicated to code that works with a ContentProvider
in a standardized way.
It also helps a ContentProvider
that handles several different types of data to keep things organized, e.g. a RailwayContentProvider
might handle trains, stations and tickets and can use the MIME type to tell each one apart.
Why MIME types?
The use of MIME types is a natural consequence when you think about how a ContentProvider
is accessed through URIs, i.e. something like an URL on the Internet. Just like on the Internet there are MIME types like text/html
for web pages and image/jpeg
for .jpg images, Android wants you to define a custom MIME type for any data type your ContentProvider
handles.
An example custom MIME type
In the NotePad (linked above) class of the NotePad example project, you'll find:
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note";
This field defines a custom MIME type (recognizable by the type/subtype
pattern).
Android suggests you use vnd.android.cursor.dir/...
as the first part for any kind of "directory listing" (multiple items) and vnd.android.cursor.item/...
as the first part for any kind of single item.
For the subtype, it's again suggested to start it with vnd.
and then add something like your reverse domain name/package name, e.g. vnd.android.cursor.item/vnd.com.mydomain.myapp.mydata
To avoid all those vnd...
strings in your code, there's also some constants in ContentResolver
like CURSOR_DIR_BASE_TYPE and CURSOR_ITEM_BASE_TYPE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With