48

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.

1
  • @Randroid If there was a downvote for comments.......
    – HB.
    Jul 20, 2018 at 9:59

3 Answers 3

81

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.

7
  • 4
    I highly recommend the NotePad example project if you want to write your own ContentProvider (posting as comment due to link limit in answer). Aug 23, 2011 at 10:43
  • Wonderful answer!!!! So we can put anything we like behind "vnd.android.cursor.dir/",right?Or it has some constraint that must be follow to avoid conflict with exist mimeTypes? Aug 23, 2011 at 11:21
  • 2
    The Android docs suggest you also start the subtype (after the /) with vnd. to mark it as "custom MIME subtype" see: What is the meaning of "vnd" in MIME types? -- after that just use reverse-domain notation (i.e. Java package names) to get a unique name. See the last the last paragraph of my answers for an example :) Aug 23, 2011 at 11:32
  • 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. Aug 23, 2011 at 11:41
  • 2
    @Philipp Reichart : great answer, thanks, it should be as clear as you answer in the doc!
    – Paul
    Dec 27, 2011 at 8:53
12

Mimetype Multipurpose Internet Mail Extensions is tell you the description of the content

Text in character sets other than ASCII

Non-text attachments

Message bodies with multiple parts

Header information in non-ASCII character sets

and also whether is it Pdf/epub/html/text etc

3

If you mean mime type its to tell the receiving entity how to interpret a file. Just like you see .txt and know a file is a text file. This way you can serve a file with .anyExtension and have the browser still know it is a .txt

1
  • 1
    Regretfull, in android it does not mean file type. Aug 23, 2011 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.