25

I put a log in getType() method it never gets printed. I am using the Notepad sample code. Please explain the 1st line of Java doc comment. Returning null from getType() is also working fine. What is the purpose of getType() method?

    /**
 * This is called when a client calls {@link android.content.ContentResolver#getType(Uri)}.
 * Returns the MIME data type of the URI given as a parameter.
 * 
 * @param uri The URI whose MIME type is desired.
 * @return The MIME type of the URI.
 * @throws IllegalArgumentException if the incoming URI pattern is invalid.
 */
@Override
public String getType(Uri uri)
{
    Log.d("Suparna", "******getType()");
    /*switch(uriMatcher.match(uri))
    {
    // ---get all books---
    case BOOK_DETAILS:
        return Book.Book_Details.CONTENT_TYPE;
        // ---get a particular book---
    case BOOK_DETAILS_ID:
        return Book.Book_Details.CONTENT_ITEM_TYPE;
    default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }*/
    return null;
}
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Android Developer
  • 559
  • 1
  • 5
  • 17

2 Answers2

37

getType(Uri uri) will usually only be called after a call to ContentResolver#getType(Uri uri). It is used by applications (either other third-party applications, if your ContentProvider has been exported, or your own) to retrieve the MIME type of the given content URL. If your app isn't concerned with the data's MIME type, it's perfectly fine to simply have the method return null.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • Ok. Like... depending on the data type of the Uri, if you want to do specific thing with the retrieved data like opening a browser or mail. I think this is the probable use case. Thanks!!! – Android Developer Sep 10 '12 at 03:47
  • 1
    Not implementing getType(Uri) is lazy at best. It **may** be okay only if you are the only client of your provider. Otherwise, you're making assumptions about other people's usage. – spaaarky21 Jul 08 '14 at 20:26
  • 2
    Actually instead of returning NULL it's better to write something like: **throw new UnsupportedOperationException("getType is not implemented");** – zkvarz Oct 25 '16 at 15:37
  • How does such an Exception raised in a ContentProvider propagate back to the calling client (the ContentResolver)? – JohnyTex May 05 '17 at 10:31
  • @JohnyTex It will only get thrown if you call it. – Alex Lockwood May 05 '17 at 15:28
  • Exception in my ContentProvider causes it to crash and not propagate back to caller. What I am doing wrong? – JohnyTex May 05 '17 at 15:30
  • @AlexLockwood `getType()` only gets called if we pass a valid `URI`. On passing any other `URI`, it simply returns `null` and doesn't even invoke `getType()` implemented in my `ContentProvider`. Then what is the use of it? – CopsOnRoad Feb 09 '18 at 07:53
4

This ContentProvider's getType() method is used mostly when you allow your ContentProvider to interact with other third party applications. This MIME Type is used by Android System to find which applications can handle it.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440