Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ContentProvider getType() called when and why

Tags:

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; } 
like image 560
Android Developer Avatar asked Sep 06 '12 10:09

Android Developer


People also ask

What is a ContentProvider and what do you use it for?

A content provider manages access to a central repository of data. A provider is part of an Android application, which often provides its own UI for working with the data. However, content providers are primarily intended to be used by other applications, which access the provider using a provider client object.

How do you read data from a content provider?

To access the data from a content provider, URI is used as a query string. Details of different parts of Content URI: content:// – Mandatory part of the URI as it represents that the given URI is a Content URI. authority – Signifies the name of the content provider like contacts, browser, etc.

What is the purpose of content provider class?

Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications.

How can I access my content provider from another application?

After creating the content provider , specify the content provider in the manifest file. You can mention content provider using the tag. Inside the provider tag dont forget to mention the name and authorities attributes. This declaration should be ..


2 Answers

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.

like image 124
Alex Lockwood Avatar answered Sep 21 '22 02:09

Alex Lockwood


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.

like image 43
Vignesh Baskaran Avatar answered Sep 21 '22 02:09

Vignesh Baskaran