Android调用系统安装程序打开本地文件(包括 Android7.0以上)
前言
在 Android 手機文件管理中,點擊某個文件的時候,會彈出選擇打開文件的方式,那么,如果在我們自己的軟件中要實現這種效果該怎么做呢?其實 Android 打開本地文件很常見,打開的時候會根據不同的文件類型來執行,所以需要先判斷文件的MIME 類型,在網上查了很多資料,實現了自己想要的效果,這里做個總結。
正文
首先來看看以下代碼,原文地址在這里,我在此技術上多添加了一些文件類型。代碼如下:
private static final String[][] MIME_MapTable={//{后綴名, MIME類型}{".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/msword"},{".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"},{".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.ms-powerpoint"},{".prop", "text/plain"},{".rar", "application/x-rar-compressed"},{".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/xml"},{".xml", "text/plain"},{".z", "application/x-compress"},{".zip", "application/zip"},{"", "*/*"}};private String getMIMEType(File file) {String type="*/*";String fName = file.getName();//獲取后綴名前的分隔符"."在fName中的位置。int dotIndex = fName.lastIndexOf(".");if(dotIndex < 0)return type;/* 獲取文件的后綴名 */String fileType = fName.substring(dotIndex,fName.length()).toLowerCase();if(fileType == null || "".equals(fileType))return type;//在MIME和文件類型的匹配表中找到對應的MIME類型。for(int i=0;i<MIME_MapTable.length;i++){if(fileType.equals(MIME_MapTable[i][0]))type = MIME_MapTable[i][1];}return type;}上面兩個方法是用于判斷文件類型的。那么接下來看看如何調用并打開文件:
public static void openAndroidFile(String filepath){Intent intent = new Intent();File file = new File(filepath); // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//設置標記intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setAction(Intent.ACTION_VIEW);//動作,查看intent.setDataAndType(Uri.fromFile(file), m_instance.getMIMEType(file));//設置類型m_instance.startActivity(intent);}OK,通過上面的方式確實能調用本地軟件來打開文件,但是只能在 android6.0以下的系統上使用,在 Android7.0以上系統就不管用了,原因是 Android7.0開始,系統禁止不安全路徑被外部訪問,所以增加了文件的權限檢查。那么怎么解決這個問題呢,比較正常的方式就是在去申請文件權限,整體操作比較麻煩,可以參考這篇文章,而還可以通過另外一種比較暴力的方式來解決該問題,那就是直接繞開系統檢查文件的權限,具體操作如下,將打開文件的地方加上兩句話,如下:
public static void openAndroidFile(String filepath){Intent intent = new Intent();// 這是比較流氓的方法,繞過7.0的文件權限檢查if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();StrictMode.setVmPolicy(builder.build());}File file = new File(filepath); // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//設置標記intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);intent.setAction(Intent.ACTION_VIEW);//動作,查看intent.setDataAndType(Uri.fromFile(file), m_instance.getMIMEType(file));//設置類型m_instance.startActivity(intent);}這里先判斷系統版本,是否為 android7.0以上,然后再添加相應語句。這種方式最簡單直接,編譯后能立馬正常打開文件。當然,這是種走捷徑的方案,隨著 android 版本的升級,不知道今后會不會不管用,畢竟牽扯系統安全問題,要不然 android 做那么多限制就沒啥意義了。所以如果說項目不是那么急的話 可以用正常的方式來解決在 android7.0以上的設備打開文件問題。
參考文章:https://www.cnblogs.com/fwling/p/7239983.html
總結
以上是生活随笔為你收集整理的Android调用系统安装程序打开本地文件(包括 Android7.0以上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的机械-电气-视觉-软件之路
- 下一篇: 移动通信设备 bbu prru 宏