Usage and types of Android MimeType
Mime type description
Multi-purpose Internet Mail Extension (MIME, Multipurpose Internet Mail Extensions) is an Internet standard that extends the email standard, it supports mail messages in multiple formats, such as non-ASCII characters and binary format attachments.
Content-Type. This header field is used to specify the message Type. Generally. [Type]/[subtype]
Type has the following format.
- Text: indicates the standardized representation of Text information. Text messages can be in multiple character sets or formats;
- Multipart: connects multiple parts of the message body to form a message. These parts can be different types of data;
- Application: used to transmit Application data or binary data;
- Message: Used to package an E-mail Message;
- Image: used to transmit static Image data;
- Audio: used to transmit Audio or Audio data;
- Video: used to transmit dynamic image data. It can be a Video data format edited together with audio.
Subtype is used to specify the type details. The set of content-type/subtype pairs and related parameters will increase over time. To ensure that these values are developed in an orderly and open state, MIME uses Internet Assigned Numbers Authority (IANA) as the central registration mechanism to manage these values. Common subtype values are as follows:
- Text/plain (plain text)
- Text/html (HTML document)
- Application/xhtml + xml (XHTML document)
- Image/gif (GIF image)
- Image/jpeg (JPEG image) [image/pjpeg in PHP]
- Image/png (PNG image) [image/x-png in PHP]
- Video/mpeg (MPEG animation)
- Application/octet-stream (any binary data)
- Application/pdf (PDF)
- Application/msword (Microsoft Word file)
- Message/rfc822 (RFC 822 format)
- Multipart/alternative (HTML form and plain text form of HTML mail, the same content is expressed in different forms)
- Application/x-www-form-urlencoded (form submitted using the http post method)
- Multipart/form-data (same as above, but mainly used when the form is submitted along with file upload) Upload )---------------------------------------------------------------------------------------------------------------------------
Usage of MimeType in Android
In Intent-FilterThere is a mimeType. Its function is to tell the Android system the type of files that the Activity can process. For example, if it is set to "text/plaindomain.txt", the file can be processed.
MimeTypeMap class
The MimeTypeMap class is a class dedicated to processing mimeType.
Bytes ---------------------------------------------------------------------------------------------------------------------------
Class description and method:
[Java]View plaincopy
- Class Overview
- Two-way map that maps MIME-types to file extensions and vice versa.
- Summary
- Public Methods
- String
- GetExtensionFromMimeType (String mimeType)
- Return the registered extension for the given MIME type.
- Static String
- GetFileExtensionFromUrl (String url)
- Returns the file extension or an empty string iff there is no extension.
- String
- GetMimeTypeFromExtension (String extension)
- Return the MIME type for the given extension.
- StaticMimeTypeMap
- GetSingleton ()
- Get the singleton instance of MimeTypeMap.
- Boolean
- HasExtension (String extension)
- Return true if the given extension has a registered MIME type.
- Boolean
- HasMimeType (String mimeType)
- Return true if the given MIME type has an entry in the map. MimeTypeMap class is in singleton mode and has no public constructor. Use the getSinglton () method to obtain the MimeTypeMap object:
MimeTypeMap mimeTypeMap = MimeTypeMap. getSingleton ();Example:
[Java]View plaincopy
- Public class MainActivity extends Activity {
- Private String tag = "MainActivity ";
-
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- System. out. println (111 );
- MimeTypeMap mimeTypeMap = MimeTypeMap. getSingleton ();
-
- // Check whether the MimeType of txt exists in MimeTypeMap
- System. out. println (mimeTypeMap. hasExtension ("txt "));
-
- System. out. println (mimeTypeMap. hasMimeType ("text/html "));
- // Obtain the MimeType of the txt file type
- String extension = mimeTypeMap. getMimeTypeFromExtension ("txt ");
- System. out. println (extension );
- }
- }Bytes ---------------------------------------------------------------------------------------------------------------------------
In Android-4.2, the MimeUtils class is used to manage all supported MimeType types.[Java]View plaincopy
- Static {
- // The following table is based on/etc/mime. types data minus
- // Chemical/* MIME types and MIME types that don't map to any
- // File extensions. We also exclude top-level domain names
- // Deal with cases like:
- //
- // Mail.google.com/a/google.com
- //
- // And "active" MIME types (due to potential security issues ).
-
- Add ("application/andrew-inset", "ez ");
- Add ("application/dsptype", "tsp ");
- Add ("application/futuresplash", "spl ");
- Add ("application/hta", "hta ");
- ... Timeout ...---------------------------------------------------------------------------------------------------------------------------
How to use:
The instance code is the sample NotePad that comes with the SDK.
StartActivity (new Intent (Intent. ACTION_EDIT, uri ));
Uri: content: // com. google. provider. NotePad/notes/1
The activity to be started is
[Html]View plaincopy
- Android: theme = "@ android: style/Theme. Light"
- Android: label = "@ string/title_note"
- Android: screenOrientation = "sensor"
- Android: configChanges = "keyboardHidden | orientation"
- >
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
How does the invisible Intent find its specific Activity?
1. The system obtains the uri from intent and obtains content: // com. google. provider. NotePad/notes/1,
Remove the starting content: identifier to get com. google. provider. NotePad/notes/1,
Then retrieve com. google. provider. NotePad from the front, and go to Androidmanfest. xml.
Find the provider whose authorities is com. google. provider. NotePad,
Then load the content provider.
[Java]View plaincopy
- Android: authorities = "com. google. provider. NotePad"
- />
2. Call the gettype function of NotePadProvider and pass the above URI to this function,
The function returns the type of the URI. Here, the return Notes. CONTENT_ITEM_TYPE indicates a log record,
CONTENT_ITEM_TYPE = "vnd. android. cursor. item/vnd. google. note"
[Java]View plaincopy
- @ Override
- Public String getType (Uri uri ){
- Switch (sUriMatcher. match (uri )){
- Case NOTES:
- Return Notes. CONTENT_TYPE;
- Case NOTE_ID:
- Return Notes. CONTENT_ITEM_TYPE;
- Default:
- Throw new IllegalArgumentException ("Unknown URI" + uri );
- }
- }
3. Then the system uses the obtained "vnd. android. cursor. item/vnd. google. note" and
"Android. intent. action. EDIT" to androidmanfest. xml to find the matching activity.
Android: authorities = "com. google. provider. NotePad" indicates the authorities of the ContentProvider,
Similar to the action in IntentFilter in activity, to put it bluntly, this ContentProvider is
The name in the android system. After the application is started,
It will always exist in the android system until the application is uninstalled.