Android ContentProvider getType() called when and why
Asked Answered
P

2

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;
}
Posturize answered 6/9, 2012 at 10:0 Comment(2)
The reason why this question didn't get any attention is because you forgot to tag it with "android". :)Opia
#5352169 may be help u.Villeneuve
O
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.

Opia answered 9/9, 2012 at 21:3 Comment(7)
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!!!Posturize
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.Lonne
Actually instead of returning NULL it's better to write something like: throw new UnsupportedOperationException("getType is not implemented");Vaticination
How does such an Exception raised in a ContentProvider propagate back to the calling client (the ContentResolver)?Mahout
@Mahout It will only get thrown if you call it.Opia
Exception in my ContentProvider causes it to crash and not propagate back to caller. What I am doing wrong?Mahout
@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?Ballyhoo
A
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.

Arthropod answered 29/12, 2016 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.