Android ContentProvider getType() called when and why

Android_Developer picture Android_Developer · Sep 6, 2012 · Viewed 10k times · Source

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;
}

Answer

Alex Lockwood picture Alex Lockwood · Sep 9, 2012

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.