MIME Type and getType () shared by the ContentProvider Database ()

Source: Internet
Author: User
This article has no preface ...... 1. ContentProvider database sharing-Overview 2. ContentProvider database sharing-instance explanation 3. ContentProvider database sharing-MIME type and getType () 4. ContentProvider database sharing-read and write permissions and data listening

This article has no preface ...... 1. ContentProvider database sharing-Overview 2. ContentProvider database sharing-instance explanation 3. ContentProvider database sharing-MIME type and getType () 4. ContentProvider database sharing-read and write permissions and data listening

This article has no preface ......



1. ContentProvider database sharing-Overview
2. ContentProvider database sharing-instance description
3. ContentProvider database sharing-MIME type and getType ()
4. ContentProvider database sharing-read and write permissions and data listening


I. Overview

In the previous article, we talked about adding, deleting, modifying, and querying shared databases. However, when generating the PeopleContentProvider class, because it is derived from ContentProvider, therefore, we have rewritten the insert (), query (), update (), and delete () functions for database operations. However, for getType (), null is directly returned; this article describes how to use the getType () function.

Let's take a look at the official description of getType:

public abstract String getType (Uri uri)Implement this to handle requests for the MIME type of the data at the given URI. The returned MIME type should start with vnd.android.cursor.item for a single record, or vnd.android.cursor.dir/ for multiple items. This method can be called from multiple threads, as described in Processes and Threads.Parametersurithe URI to query.Returnsa MIME type string, or null if there is no type.
In general, it is to pass in a URI and return a string that represents the MIME type. It also says that if it is a single record, it should be returned with vnd. android. cursor. item/is the first string. If multiple records exist, vnd is returned. android. cursor. the string headed by dir;
Next, let's take a look at the MIME type?
2. MIME Type 1. What is MIME type?

According to Baidu encyclopedia's explanation: MIME: Full name: Multipurpose Internet Mail Extensions, multi-function Internet Mail Extension Service. It is a multi-purpose Internet Mail extended protocol. It was first used in the email system in 1992, but later applied to browsers. The MIME type is the type used by an application to open a file with a certain extension. When a file with the extension is accessed, the browser automatically opens the file with the specified application. It is used to specify custom client file names and open media files.
After reading it, there is only one feeling ...... Still don't understand! Simply put, the MIME type is used to identify the file type that can be opened by the current Activity!

Below is a simple list of the built-in file types and the corresponding MIME types:

(The first is the file name, followed by the corresponding MIME-type string)

{". Bmp", "image/bmp "}
{". C", "text/plain "}
{". Class", "application/octet-stream "}
{". Conf", "text/plain "}
{". Cpp", "text/plain "}
{". Doc", "application/msword "}

2. What is the MIME type used?

Now let's take a look at what the MIME type is for in android?
First, the MIME type is mainly the data field of the Intent-filter of the Activity. For example, the following Activity:

    
                 
          
      
 
The value of MimeType in the data field is "image/bmp". That is, this Activity can be enabled only when the value of MimeType is "image/bmp" in implicit Intent matching, that is to say, this Activity can only open files of the image/bmp Type !!!! This is the focus of MIME type matching;
The MIME type is used to specify in the Activity. The current Activity supports the file type to be opened !!
3. getType () 1. Overview

Now let's look back at the getType () function in ContentProvider. This function will generate a string representing MimeType Based on the passed URI. The generation of this string also has rules:

  • If it is a single record, a string headed by vnd. android. cursor. item/should be returned.
  • If multiple records exist, the vnd. android. cursor. dir/string is returned.
As for the character string/suffix, you can define it as needed.

Here is a question. Why does the returned MimeType start with vnd. android. cursor. item/or vnd. android. cursor. dir?
We know that the MIME type is actually a string separated by a slash (/). The front part of the slash (/) is the part recognized by the system, it is equivalent to the variable data type when we define a variable. Through this "Data Type", the system can know what we want to represent. As for the "/", we define the "variable name.

2. Relationship between getType () and Activity

As we have discussed above, MIME exists in the intent-filter of the Activity. What is the relationship between our getType () and the intent-filter of the Activity?
In fact, the MIME type returned by getType () is mainly used to implicitly match the Intent MIMETYPE field to start the Activity.
Let's take a look at how to enable Activity through URI:

Intent intent = new Intent();intent.setAction("harvic.test.qijian");intent.setData(mCurrentURI);startActivity(intent);
Where:
public static final String AUTHORITY = "com.harvic.provider.PeopleContentProvider";public static final Uri CONTENT_URI_FIRST = Uri.parse("content://" + AUTHORITY + "/first");public static Uri mCurrentURI = CONTENT_URI_FIRST;
In the above Code, we set action and content uri;
Here, how does one enable implicit Activity using Content URI?

  • (1) first, third-party applications use content Uri and action to implicitly match Intent to enable Activity.
Intent intent = new Intent();intent.setAction("harvic.test.qijian");intent.setData(mCurrentURI);startActivity(intent);
  • (2) The system matches ContentProvider through Authority in URI to find our PeopleContentProvider.
  • (3) Find lelecontentprovider. Because we are matching the Intent, getType (uri) is called to return the URI type:
static {     sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);     sUriMatcher.addURI(AUTHORITY, "first", MATCH_FIRST);     sUriMatcher.addURI(AUTHORITY, "second", MATCH_SECOND);    }
The above is the UriMather constructor. The code above shows that when "/first" is matched, MATCH_FIRST is returned, that is, the value 1. If "/second" is matched, MATCH_SECOND is returned, that is, the value 2.
Therefore:
1. When "/fist" is matched, the custom MIME type: vnd. android. cursor. dir/harvic. first is returned.
2. When "/second" is matched, the returned MIME type is vnd. android. cursor. item/harvic. second.
The Code is as follows:
public static final String CONTENT_FIRST_TYPE = "vnd.android.cursor.dir/harvic.first";public static final String CONTENT_SECOND_TYPE = "vnd.android.cursor.item/harvic.second";
public String getType(Uri uri) {    switch (sUriMatcher.match(uri)){        case MATCH_FIRST:{            return CONTENT_FIRST_TYPE;        }        case MATCH_SECOND:{            return CONTENT_SECOND_TYPE;        }    }    return null;}
  • (4) The following describes how to match Intent based on the Action and MIME types.
Currently, no Activity in the ContentProviderBlog project can match the Action and MIME type, so we create a new SecondActivity;
3. Create a SecondActivity to start in this Activity through URI. We only use TextView to identify the current Activity from the secondActivity of ContentProviderBlog;
XML code:Therefore, in TextView, we change the Text attribute to "ContentProviderBlog's secondActivity" to identify this Activity;
     
  
 
4. AndroidManifest. xml in AndroidManifest. in xml, add Intent-filter required for implicit matching on SecondActivity. Note that two MIME types are returned in getType () based on different Uris, here, only one mimeType: vnd is added to the data field of SecondActivity. android. cursor. dir/harvic. first; that is, when we use content: // com. harvic. provider. secondActivity cannot be enabled when PeopleContentProvider/second implicitly matches Intent, because the MIME type does not match!
    
                 
          
      
 
4. The result is displayed. Let's see what results will appear when different Uris are used to enable the Activity;

Use content: // com. harvic. provider. PeopleContentProvider/first. The result is as follows:
Click "thirdPart" and call Activity through URI


Using content: // com. harvic. provider. PeopleContentProvider/second, the Activity cannot be called because the MIME does not match.



Similarly, the source code contains two parts:
(Install ContentProviderBlog first, and then UseProvider; Use UseProvider to operate the database of ContentProviderBlog and check the LOG)
1. ContentProviderBlog: This is an APP that provides shared database interfaces;
2. UseProvider: A third party uses URI to operate the database APP;

If this article helps you, remember to pay attention to it.

Source Code address: http://download.csdn.net/detail/harvic880925/8532205

Http://blog.csdn.net/harvic880925/article/details/44620851 thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.