Android calls system installed software to open file (go)

Source: Internet
Author: User
Tags gtar

Android calls system installed software to open file (go)

In the application how to call the system installed software to open a file, this is the problem we often encounter, the following is a way I used to share with you!

This is a way to open the file:

Java code
  1. /**
  2. * Open File
  3. * @param file
  4. */
  5. Private void OpenFile (file file) {
  6. Intent Intent = new Intent ();
  7. Intent.addflags (Intent.flag_activity_new_task);
  8. //Set the Action property of Intent
  9. Intent.setaction (Intent.action_view);
  10. //Get MIME type of file
  11. String type = getmimetype (file);
  12. //Set the data and type properties of the intent.
  13. Intent.setdataandtype (/*uri*/uri.fromfile (file), type);
  14. //Jump
  15. StartActivity (Intent);
  16. }
  17. /**
  18. * The corresponding MIME type is obtained according to the file suffix name.
  19. * @param file
  20. */
  21. Private String getmimetype (file file) {
  22. String type="*/*";
  23. String fName = File.getname ();
  24. //Get the delimiter "." Before the suffix name.  The position in the fname.
  25. int dotindex = Fname.lastindexof (".");
  26. if (Dotindex < 0) {
  27. return type;
  28. }
  29. /* Get the suffix name of the file */
  30. String end=fname.substring (Dotindex,fname.length ()). toLowerCase ();
  31. if (end=="")return type;
  32. //The corresponding MIME type is found in the MIME and file type match table.
  33. For (int i=0;i<mime_maptable.length;i++) { //mime_maptable??  You must have a question here, what is this mime_maptable?
  34. if (end.equals (mime_maptable[i][0]))
  35. Type = mime_maptable[i][1];
  36. }
  37. return type;
  38. }

Mime_maptable is a string array of MIME types that correspond to the suffix names of all files:

Java code
  1. Private final string[][] mime_maptable={
  2. //{suffix name, MIME type}
  3. {". 3gp", "VIDEO/3GPP"},
  4. {". apk", "Application/vnd.android.package-archive"},
  5. {". asf", "video/x-ms-asf"},
  6. {". avi", "Video/x-msvideo"},
  7. {". Bin", "Application/octet-stream"},
  8. {". bmp", "Image/bmp"},
  9. {". C", "Text/plain"},
  10. {". Class", "Application/octet-stream"},
  11. {". conf", "Text/plain"},
  12. {". cpp", "Text/plain"},
  13. {". doc", "Application/msword"},
  14. {". docx", "Application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
  15. {". xls", "application/vnd.ms-excel"},
  16. {". xlsx", "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
  17. {". exe", "Application/octet-stream"},
  18. {". gif", "Image/gif"},
  19. {". Gtar", "Application/x-gtar"},
  20. {". Gz", "Application/x-gzip"},
  21. {". h", "Text/plain"},
  22. {". htm", "text/html"},
  23. {". html", "text/html"},
  24. {". Jar", "Application/java-archive"},
  25. {". Java", "Text/plain"},
  26. {". jpeg", "Image/jpeg"},
  27. {". jpg", "Image/jpeg"},
  28. {". js", "Application/x-javascript"},
  29. {". Log", "Text/plain"},
  30. {". m3u", "Audio/x-mpegurl"},
  31. {". m4a", "Audio/mp4a-latm"},
  32. {". m4b", "Audio/mp4a-latm"},
  33. {". m4p", "Audio/mp4a-latm"},
  34. {". M4u", "Video/vnd.mpegurl"},
  35. {". m4v", "video/x-m4v"},
  36. {". mov", "Video/quicktime"},
  37. {". Mp2", "Audio/x-mpeg"},
  38. {". mp3", "Audio/x-mpeg"},
  39. {". mp4", "Video/mp4"},
  40. {". MPC", "Application/vnd.mpohun.certificate"},
  41. {". Mpe", "Video/mpeg"},
  42. {". Mpeg", "Video/mpeg"},
  43. {". mpg", "Video/mpeg"},
  44. {". Mpg4", "Video/mp4"},
  45. {". MPGA", "Audio/mpeg"},
  46. {". Msg", "Application/vnd.ms-outlook"},
  47. {". ogg", "Audio/ogg"},
  48. {". pdf", "Application/pdf"},
  49. {". png", "Image/png"},
  50. {". pps", "Application/vnd.ms-powerpoint"},
  51. {". ppt", "Application/vnd.ms-powerpoint"},
  52. {". pptx", "Application/vnd.openxmlformats-officedocument.presentationml.presentation"},
  53. {". Prop", "Text/plain"},
  54. {". rc", "Text/plain"},
  55. {". rmvb", "Audio/x-pn-realaudio"},
  56. {". rtf", "Application/rtf"},
  57. {". Sh", "Text/plain"},
  58. {". Tar", "Application/x-tar"},
  59. {". tgz", "application/x-compressed"},
  60. {". txt", "Text/plain"},
  61. {". wav", "Audio/x-wav"},
  62. {". wma", "Audio/x-ms-wma"},
  63. {". wmv", "audio/x-ms-wmv"},
  64. {". WPs", "Application/vnd.ms-works"},
  65. {". xml", "Text/plain"},
  66. {". Z", "Application/x-compress"},
  67. {". zip", "application/x-zip-compressed"},
  68. {"",        "*/*"}    
  69. };

This MIME type may not be complete, do you have anything to add?

Original: http://tonysun3544.iteye.com/blog/1265884

Android calls system installed software to open file (go)

Related Article

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.