Android calls third-party software to open the downloaded attachment

Source: Internet
Author: User
Tags gtar
Android calls third-party software to open the downloaded attachment
When creating our product, we need to download the attachment. At the same time, we need to open the attachment. After reading some information, we found that android is doing very well with this support. Through anction, we can add minitype, after adding a data source, you can find the appropriate software to open your downloaded attachment. The following is a Java class I have compiled. I hope it will help you!
 
/** @ Project c6client * @ package COM. invalid. c6.util * @ file callotheropeanfile. java * @ version 1.0 * @ author liaoyp * @ time 6:30:54 **/package COM. invalid. c6.util; import Java. io. file; import android. content. activitynotfoundexception; import android. content. context; import android. content. intent; import android.net. uri; import android. widget. toast; public class callotheropeanfile {/***** <code> openf Ile </code> * @ Description: Todo (open the attachment) * @ Param context * @ Param file * @ since 2012-5-19 liaoyp */Public void openfile (context, file) {try {intent = new intent (); intent. addflags (intent. flag_activity_new_task); // sets the intent action attribute intent. setaction (intent. action_view); // obtain the MIME type string type = getmimetype (File) of the file; // set the data and type attributes of the intent. Intent. setdataandtype (/* URI */uri. fromfile (file), type); // jump to context. startactivity (intent); // intent. createchooser (intent, "select the corresponding software to open this attachment! ");} Catch (activitynotfoundexception e) {// todo: handle exception toast. maketext (context," the sorry attachment cannot be opened. Please download the relevant software! ", 500 ). show () ;}} private string getmimetype (File file) {string type = "*/*"; string fname = file. getname (); // get the separator before the suffix ". "position 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 and file type matching tables. For (INT I = 0; I <mime_maptable.length; I ++ ){
        if(end.equals(MIME_MapTable[i][0]))             type = MIME_MapTable[i][1];     }            return type; } 
// You can add private string [] [] mime_maptable = {// {suffix name, 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 "},{". MCM "," 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 "},{"","*/*"}};}

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.