Android open various files (setDataAndType)

Source: Internet
Author: User
Tags gtar

Android open various files (setDataAndType)

Java code /**

* Open a file

* @ Param file

*/

Private void openFile (File file ){

Intent intent = new Intent ();

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

// Set the intent Action attribute

Intent. setAction (Intent. ACTION_VIEW );

// Obtain the MIME type of the file

String type = getMIMEType (file );

// Set the data and Type attributes of intent.

Intent. setDataAndType (/* uri */Uri. fromFile (file), type );

// Jump

StartActivity (intent); // try it here, and an error may be reported. // For example, if your MIME type is to open the mailbox, but the mailbox client is not installed on your mobile phone, an error will be reported.

}

/**

* Obtain the corresponding MIME type based on the file suffix.

* @ Param file

*/

Private String getMIMEType (File file ){

String type = "*/*";

String fName = file. getName ();

// Obtain the position of the separator "." Before the Suffix in fName.

Int dotIndex = fName. lastIndexOf (".");

If (dotIndex <0 ){

Return type;

}

/* Get the file suffix */

String end = fName. substring (dotIndex, fName. length (). toLowerCase ();

If (end = "") return type;

// Find the corresponding MIME type in the MIME-object type matching table.

For (int I = 0; I if (end. equals (MIME_MapTable [I] [0])

Type = MIME_MapTable [I] [1];

}

Return type;

}

MIME_MapTable is a String array of the MIME type corresponding to the suffix of all files:

Java code private final String [] [] MIME_MapTable = {

// {Suffix, MIME type}

{". 3gp", "video/3gpp "},

{". Apk", "application/vnd. android. package-archive "},

{". Asf", "video/x-ms-asf "},

{". Avi", "video/x-msvideo "},

{". Bin", "application/octet-stream "},

{". Bmp", "image/bmp "},

{". C", "text/plain "},

{". Class", "application/octet-stream "},

{". Conf", "text/plain "},

{". Cpp", "text/plain "},

{". Doc", "application/msword "},

{". Docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument "},

{". Xls", "application/vnd. ms-excel "},

{". Xlsx", "application/vnd. openxmlformats-officedocument.spreadsheetml.sheet "},

{". Exe", "application/octet-stream "},

{". Gif", "image/gif "},

{". Gtar", "application/x-gtar "},

{". Gz", "application/x-gzip "},

{". H", "text/plain "},

{". Htm", "text/html "},

{". Html", "text/html "},

{". Jar", "application/java-archive "},

{". Java", "text/plain "},

{". Jpeg", "image/jpeg "},

{". Jpg", "image/jpeg "},

{". Js", "application/x-javascript "},

{". Log", "text/plain "},

{". M3u", "audio/x-mpegurl "},

{". M4a", "audio/mp4a-latm "},

{". M4b", "audio/mp4a-latm "},

{". M4p", "audio/mp4a-latm "},

{". M4u", "video/vnd. mpegurl "},

{". M4v", "video/x-m4v "},

{". Mov", "video/quicktime "},

{". Mp2", "audio/x-mpeg "},

{". Mp3", "audio/x-mpeg "},

{". Mp4", "video/mp4 "},

{". Mpc", "application/vnd. mpohun. certificate "},

{". Mpe", "video/mpeg "},

{". Mpeg", "video/mpeg "},

{". Mpg", "video/mpeg "},

{". Mpg4", "video/mp4 "},

{". Mpga", "audio/mpeg "},

{". Msg", "application/vnd. ms-outlook "},

{". Ogg", "audio/ogg "},

{". Pdf", "application/pdf "},

{". Png", "image/png "},

{". Pps", "application/vnd. ms-powerpoint "},

{". Ppt", "application/vnd. ms-powerpoint "},

{". Pptx", "application/vnd. openxmlformats-officedocument.presentationml.presentation "},

{". Prop", "text/plain "},

{". Rc", "text/plain "},

{". Rmvb", "audio/x-pn-realaudio "},

{". Rtf", "application/rtf "},

{". Sh", "text/plain "},

{". Tar", "application/x-tar "},

{". Tgz", "application/x-compressed "},

{". Txt", "text/plain "},

{". Wav", "audio/x-wav "},

{". Wma", "audio/x-ms-wma "},

{". Wmv", "audio/x-ms-wmv "},

{". Wps", "application/vnd. ms-works "},

{". Xml", "text/plain "},

{". Z", "application/x-compress "},

{". Zip", "application/x-zip-compressed "},

{"","*/*"}

};

This MIME type may not be complete. Do you want to add it?

Certificate certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. Intent open a picture file public:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

Uriuri = Uri. fromFile (new

File ("/mnt/sdcard/images/001010980.jpg "));

Intent. setDataAndType (uri, "image /*");

This. startActivity (intent );

2. Intent to open a PDF file:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

Uriuri = Uri. fromFile (new

File ("file: // android_asset/helphelp.pdf "));

Intent. setDataAndType (uri, "application/pdf ");

This. startActivity (intent );

3. Intent to open a text file:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

If (paramBoolean)

{

Uriuri1 = Uri. parse (param );

Intent. setDataAndType (URI1, "text/plain ");

}

Else

{

Uriuri = Uri. fromFile (newFile ("/mnt/sdcard/hello.txt "));

Intent. setDataAndType (URI2, "text/plain ");

}

This. startActivity (intent );

4. Intent to open the audio file:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP );

Intent. putExtra ("oneshot", 0 );

Intent. putExtra ("configchange", 0 );

Uriuri = Uri. fromFile (newFile ("/mnt/sdcard/renew "));

Intent. setDataAndType (uri, "audio /*");

This. startActivity (intent );

5. Intent to open the video file:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP );

Intent. putExtra ("oneshot", 0 );

Intent. putExtra ("configchange", 0 );

Uriuri = Uri. fromFile (newFile ("/mnt/sdcard/ice. avi "));

Intent. setDataAndType (uri, "video /*");

This. startActivity (intent );

6. Intent to open the CHM file:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

Uriuri = Uri. fromFile (newFile ("/mnt/sdcard/ice. chm "));

Intent. setDataAndType (uri, "application/x-chm ");

This. startActivity (intent );

7. Intent to open a Word document:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

Uriuri = Uri. fromFile (newFile ("/system/etc/help.doc "));

Intent. setDataAndType (uri, "application/msword ");

This. startActivity (intent );

8. Android Excel intent:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

Uriuri = Uri. fromFile (newFile ("/mnt/sdcard/Book1.xls "));

Intent. setDataAndType (uri, "application/vnd. ms-excel ");

This. startActivity (intent );

9. Intent to open the PPT file:

Java code

 

Intentintent = newIntent ("android. intent. action. VIEW ");

Intent. addCategory ("android. intent. category. DEFAULT ");

Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK );

Uriuri = Uri. fromFile (new

File ("/mnt/sdcard/download/Android_PPT.ppt "));

Intent. setDataAndType (uri, "application/vnd. ms-powerpoint ");

This. startActivity (intent );

10. Display Html page ::

Java code

 

Uriuri = Uri. parse ("http://www.google.com ");

Intentintent = newIntent (Intent. ACTION_VIEW, uri );

This. startActivity (intent );

11. Show map:

Java code

 

Uriuri = Uri. parse ("geo: 38.899533,-77.036476 ");

Intentintent = newIntent (Intent. Action_VIEW, uri );

This. startActivity (intent );

12. Call the dialer:

Java code

 

Uriuri = Uri. parse ("tel: xxxxxx ");

Intentintent = newIntent (Intent. ACTION_DIAL, uri );

This. startActivity (intent );

13. Call:

Java code

 

Uriuri = Uri. parse ("tel: xxxxxx ");

Intentit = newIntent (Intent. ACTION_CALL, uri );

This. startActivity (intent );

/* Permission:

*/

14. Call to send text messages of the program:

Java code

 

Intentintent = newIntent (Intent. ACTION_VIEW );

Intent. putExtra ("sms_body", "TheSMStext ");

Intent. setType ("vnd. android-dir/mms-sms ");

This. startActivity (intent );

15. Send SMS:

Java code

 

Url = Uri. parse ("smsto: 0800000123 ");

Intentintent = newIntent (Intent. ACTION_SENDTO, uri );

Intent. putExtra ("sms_body", "TheSMStext ");

This. startActivity (intent );

16. Send MMS:

Java code

 

Uriuri = Uri. parse ("content: // media/external/images/media/23 ");

Intentintent = newIntent (Intent. ACTION_SEND );

Intent. putExtra ("sms_body", "sometext ");

Intent. putExtra (Intent. EXTRA_STREAM, uri );

Intent. setType ("image/png ");

This. startActivity (intent );

17. Send an Email:

Java code

 

Uriuri = Uri. parse ("mailto: xxx@abc.com ");

Intentintent = newIntent (Intent. ACTION_SENDTO, uri );

This. startActivity (intent );

18. Send an Email with body:

Java code

 

Intentintent = newIntent (Intent. ACTION_SEND );

Intent. putExtra (Intent. EXTRA_EMAIL, "me@abc.com ");

Intent. putExtra (Intent. EXTRA_TEXT, "Theemailbodytext ");

Intent. setType ("text/plain ");

This. startActivity (

Intent. createChooser (intent, "ChooseEmailClient "));

19. Send an Email with body, to, cc:

Java code

 

Intentintent = newIntent (Intent. ACTION_SEND );

String [] tos = {"me@abc.com "};

String [] ccs = {"you@abc.com "};

Intent. putExtra (Intent. EXTRA_EMAIL, tos );

Intent. putExtra (Intent. EXTRA_CC, ccs );

Intent. putExtra (Intent. EXTRA_TEXT, "Theemailbodytext ");

Intent. putExtra (Intent. EXTRA_SUBJECT, "Theemailsubjecttext ");

Intent. setType ("message/rfc822 ");

This. startActivity (

Intent. createChooser (intent, "ChooseEmailClient "));

20. Send an Email with attachments:

Java code

 

Intentintent = newIntent (Intent. ACTION_SEND );

Intent. putExtra (Intent. EXTRA_SUBJECT, "Theemailsubjecttext ");

Intent. putExtra (Intent. EXTRA_STREAM, "file: // sdcard/mysong.mp3 ");

SendIntent. setType ("audio/mp3 ");

This. startActivity (

Intent. createChooser (intent, "ChooseEmailClient "));

21. Uninstall the program:

Java code

 

Uriuri = Uri. fromParts ("package", strPackageName, null );

Intentintent = newIntent (Intent. ACTION_DELETE, uri );

This. startActivity (

Intent. createChooser (intent, "ChooseEmailClient "));

22. Install the apk:

Java code

 

UriinstallUri = Uri. fromParts ("package", "xxx", null );

ReturnIt = newIntent (Intent. ACTION_PACKAGE_ADDED, installUri );

This. startActivity (returnIt );

23. Search applications:

Java code

 

Uriuri = Uri. parse ("market: // search? Q = pname: pkg_name ");

Intentintent = newIntent (Intent. ACTION_VIEW, uri );

This. startActivity (intent );

// Wherepkg_nameisthefullpackagepathforanapplication

24. Google Search Launch Web Browser:

Java code

 

Intentintent = newIntent (Intent. ACTION_WEB_SEARCH );

Stringterm = "Android ";

Intent. putExtra (SearchManager. QUERY, term );

Activity. startActivity (intent );

25. Send text using Intent (to messaging apps ):

Java code

 

Intentintent = newIntent (Intent. ACTION_WEB_SEARCH );

StringmsgBody = "Thisismessage ";

Intentintent = newIntent (android. content. Intent. ACTION_SEND );

Intent. setType ("text/plain ");

Intent. putExtra (android. content. Intent. EXTRA_SUBJECT,

"Messagesubject ");

Intent. putExtra (android. content. Intent. EXTRA_TEXT, msgBody );

Activity. startActivity (Intent. createChooser (intent, getResources ().

GetString (R. string. struct _by_using )));

26. Create Shortcut on "Home Screen ":

Java code

 

Intentintent = newIntent (Intent. ACTION_WEB_SEARCH );

IntenttoPrint = newIntent (this, ancreateshucut. class );

IntentaddShortcut = newIntent

("Com. android. launcher. action. INSTALL_SHORTCUT ");

AddShortcut. putExtra (Intent. EXTRA_SHORTCUT_NAME, "Shutcutname ");

AddShortcut. putExtra (Intent. EXTRA_SHORTCUT_INTENT, toPrint );

AddShortcut. putExtra (Intent. EXTRA_SHORTCUT_ICON_RESOURCE,

Intent. Reset cuticonresource. fromContext (this, R. drawable. icon ));

Manifestfile:

Permission. INSTALL_SHORTCUT ">

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.