I’m trying to implement a database in my application. I’m following a tutorial about writing my own ContentProvider, but I’m confused about SQLiteOpenHelper::getType. We’re supposed to write it and, essentially, write a switch that returns the MIME type corresponding to the type of data we’re dealing with.
I don’t fully understand it. I’m not sure, even though I have an example, what is precisely supposed to be conveyed here. How do I determine the MIME type of my different tables of data? Could someone give me a good explanation of this?
This has nothing to do with
ContentProvider. AContentProviderdoes not need to use a database; a database does not need to be hidden by aContentProvider.There is no
getType()method onSQLiteOpenHelper. I am going to assume you are referring togetType()onContentProvider.Um…a MIME type.
For a database, you probably invent your own MIME types. Bear in mind that
ContentProvideris not only used for a table-style structure — it can be used for returning files as well. In this case, you’d try to use the MIME types appropriate to the file in question.You are presumably viewing this Web page in a Web browser. When you clicked the link or whatever that led you here, your Web browser made an HTTP request to the Web server, providing a URI of
/questions/3559053/could-someone-explain-sqliteopenhelper-gettype. The StackOverflow Web server returned the content of the page, along with the MIME type of the content itself. This latter piece is important, particularly for this URI, since there is no file extension. Without a MIME type, the Web browser would not know what to do with the results of the HTTP request.Android follows the same model.
For example, you can create an
ACTION_VIEWIntentfor aUrirepresenting a contact in theContactsContractContentProvider. However, if there were no way for Android to figure out a MIME type, you could not start an activity using the Intent. Android needs to ask theContentProvider, “yo, dawg, U gots me a MIME type fo’ this Uri?”. Then, armed with an action (ACTION_VIEW) and a MIME type, Android can find a matching activity that can process the request.