Summary of various types of files opened by Android, and android Summary
It's easy. by calling the system's intent, we can open various files. unfamiliar users can learn about action, ype, and uri.
The general method is as follows:
Public static Intent openFile (String filePath) {File file = new File (filePath); if (! File. exists () return null;/* Get the extension */String end = file. getName (). substring (file. getName (). lastIndexOf (". ") + 1, file. getName (). length ()). toLowerCase ();/* determines MimeType */if (end. equals ("m4a") | end. equals ("mp3") | end. equals ("mid") | end. equals ("xmf") | end. equals ("ogg") | end. equals ("wav") {return getAudioFileIntent (filePath);} else if (end. equals ("3gp") | end. equals ("mp4") {return getAudioFileIntent (filePath);} else if (end. equals ("jpg") | end. equals ("gif") | end. equals ("png") | end. equals ("jpeg") | end. equals ("bmp") {return getImageFileIntent (filePath);} else if (end. equals ("apk") {return getApkFileIntent (filePath);} else if (end. equals ("ppt") {return getPptFileIntent (filePath);} else if (end. equals ("xls") {return getExcelFileIntent (filePath);} else if (end. equals ("doc") {return getWordFileIntent (filePath);} else if (end. equals ("pdf") {return getPdfFileIntent (filePath);} else if (end. equals ("chm") {return getChmFileIntent (filePath);} else if (end. equals ("txt") {return getTextFileIntent (filePath, false);} else {return getAllIntent (filePath );}} // Android gets an intentpublic static Intent getAllIntent (String param) {Intent intent Intent = new intent (); Intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); intent. setAction (android. content. intent. ACTION_VIEW); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "*/*"); return intent;} // Android obtains an intentpublic static Intent getApkFileIntent (String param) used to open the APK file) {Intent intent = new Intent (); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); intent. setAction (android. content. intent. ACTION_VIEW); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/vnd. android. package-archive "); return intent;} // Android obtains an intentpublic static Intent getVideoFileIntent (String param) {Intent intent Intent = new intent (" android. intent. action. VIEW "); intent. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); intent. putExtra ("oneshot", 0); intent. putExtra ("configchange", 0); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "video/*"); return intent;} // Android obtains an intentpublic static Intent getAudioFileIntent (String param) used to open the AUDIO file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP); intent. putExtra ("oneshot", 0); intent. putExtra ("configchange", 0); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "audio/*"); return intent;} // Android obtains an intent public static Intent getHtmlFileIntent (String param) {Uri = uri. parse (param ). buildUpon (). encodedAuthority ("com.android.html fileprovider "). scheme ("content "). encodedPath (param ). build (); Intent intent = new Intent ("android. intent. action. VIEW "); intent. setDataAndType (uri, "text/html"); return intent;} // Android obtains an intentpublic static Intent getImageFileIntent (String param) used to open an image file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "image/*"); return intent;} // Android obtains an intent public static Intent getPptFileIntent (String param) used to open the pptfile) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/vnd. ms-powerpoint "); return intent;} // Android obtains an intent public static Intent getExcelFileIntent (String param) {Intent intent Intent = new intent (" android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/vnd. ms-excel "); return intent;} // Android obtains an intent public static Intent getWordFileIntent (String param) {Intent intent = new Intent (" android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/msword"); return intent;} // Android obtains an intent public static Intent getChmFileIntent (String param) used to open the CHM File) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/x-chm"); return intent;} // Android obtains an intent public static Intent getTextFileIntent (String param, boolean paramBoolean) used to open a text file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); if (paramBoolean) {Uri uri1 = Uri. parse (param); intent. setDataAndType (uri1, "text/plain");} else {Uri uri2 = Uri. fromFile (new File (param); intent. setDataAndType (uri2, "text/plain");} return intent;} // Android obtains an intent public static Intent getPdfFileIntent (String param) used to open a PDF file) {Intent intent = new Intent ("android. intent. action. VIEW "); intent. addCategory ("android. intent. category. DEFAULT "); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri. fromFile (new File (param); intent. setDataAndType (uri, "application/pdf"); return intent ;}
Above.
How can I open this file type in Android? It seems that it cannot be opened in a common method.
They are also apk programs, but they are all core programs of the system and called by the system itself. After root, you can use a third-party File Manager to find them in system/app.
How does android enable the file opening method?
For example, when you open a text file in the document viewer, a list of software that can be opened is displayed. <activityandroid: name =. easyNoteandroid: label = @ string/app_nameandroid: launchMode = singleTaskandroid: screenOrientation = portrait <intent-filter <actionandroid: name = android. intent. action. MAIN/<categoryandroid: name = android. intent. category. LAUNCHER/</intent-filter <actionandroid: name = android. intent. action. VIEW </action <categoryandroid: name = android. intent. catego Ry. DEFAULT </category <dataandroid: mimeType = text/plain </data </intent-filter </activity: the first <intent-filter tag exists in every program, the key is to add the second one! In this way, your application will appear in the open list by default... Note that you need to change mimeType to the type you need. text files are of course text/plain and other commonly used ones such: