Android開啟檔案的Intent及使用

來源:互聯網
上載者:User
出自http://my.oschina.net/yuhanxun/blog/81007

在寫檔案管理系統時會用到各種開啟不同格式的檔案的需求,由於Android系統預設內建了一些可以開啟的系統應用,但還是不能滿足需求,比如開啟視頻檔案、word等,需要安裝相應的播放軟體才可以使用,這時程式會通過Intent尋找可以使用的軟體

實現通過代碼開啟一個檔案需要2部分,一部分是要擷取到不同檔案的尾碼,以便根據需求匹配相應的Intent,另一個就是不同格式的檔案開啟的Intent不同

1、在values目錄下定義尾碼數組檔案fileendings

<?xml version="1.0" encoding="utf-8"?><resources>    <array name="fileEndingImage">        <item>.png</item>        <item>.gif</item>        <item>.jpg</item>        <item>.jpeg</item>        <item>.bmp</item>    </array>    <array name="fileEndingAudio">        <item>.mp3</item>        <item>.wav</item>        <item>.ogg</item>        <item>.midi</item>    </array>    <array name="fileEndingVideo">        <item>.mp4</item>        <item>.rmvb</item>        <item>.avi</item>        <item>.flv</item>    </array>    <array name="fileEndingPackage">        <item>.jar</item>        <item>.zip</item>        <item>.rar</item>        <item>.gz</item>        <item>.apk</item>        <item>.img</item>    </array>    <array name="fileEndingWebText">        <item>.htm</item>        <item>.html</item>        <item>.php</item>        <item>.jsp</item>    </array>    <array name="fileEndingText">        <item>.txt</item>        <item>.java</item>        <item>.c</item>        <item>.cpp</item>        <item>.py</item>        <item>.xml</item>        <item>.json</item>        <item>.log</item>    </array>    <array name="fileEndingWord">        <item>.doc</item>        <item>.docx</item>    </array>    <array name="fileEndingExcel">        <item>.xls</item>        <item>.xlsx</item>    </array>    <array name="fileEndingPPT">        <item>.ppt</item>        <item>.pptx</item>    </array>    <array name="fileEndingPdf">        <item>.pdf</item>    </array></resources>

2、定義OpenFiles工具類,只需傳輸File參數即可,然後通過返回的Intent開啟檔案

public class OpenFiles {     //android擷取一個用於開啟HTML檔案的intent       public static Intent getHtmlFileIntent(File file)       {           Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();           Intent intent = new Intent("android.intent.action.VIEW");           intent.setDataAndType(uri, "text/html");           return intent;       }     //android擷取一個用於開啟圖片檔案的intent       public static Intent getImageFileIntent(File 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(file);           intent.setDataAndType(uri, "image/*");           return intent;       }       //android擷取一個用於開啟PDF檔案的intent       public static Intent getPdfFileIntent(File 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(file);         intent.setDataAndType(uri, "application/pdf");         return intent;       }     //android擷取一個用於開啟文字檔的intent     public static Intent getTextFileIntent(File 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(file);       intent.setDataAndType(uri, "text/plain");       return intent;     }         //android擷取一個用於開啟音頻檔案的intent       public static Intent getAudioFileIntent(File 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(file);         intent.setDataAndType(uri, "audio/*");         return intent;       }       //android擷取一個用於開啟視頻檔案的intent       public static Intent getVideoFileIntent(File 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(file);         intent.setDataAndType(uri, "video/*");         return intent;       }               //android擷取一個用於開啟CHM檔案的intent       public static Intent getChmFileIntent(File 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(file);         intent.setDataAndType(uri, "application/x-chm");         return intent;       }             //android擷取一個用於開啟Word檔案的intent       public static Intent getWordFileIntent(File 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(file);         intent.setDataAndType(uri, "application/msword");         return intent;       }     //android擷取一個用於開啟Excel檔案的intent       public static Intent getExcelFileIntent(File 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(file);         intent.setDataAndType(uri, "application/vnd.ms-excel");         return intent;       }     //android擷取一個用於開啟PPT檔案的intent       public static Intent getPPTFileIntent(File 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(file);         intent.setDataAndType(uri, "application/vnd.ms-powerpoint");         return intent;       }       //android擷取一個用於開啟apk檔案的intent       public static Intent getApkFileIntent(File file)       {           Intent intent = new Intent();             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             intent.setAction(android.content.Intent.ACTION_VIEW);             intent.setDataAndType(Uri.fromFile(file),  "application/vnd.android.package-archive");             return intent;       }              }

3、定義用於檢查要開啟的檔案的尾碼是否在遍曆尾碼數組中


private boolean checkEndsWithInStringArray(String checkItsEnd,                     String[] fileEndings){        for(String aEnd : fileEndings){            if(checkItsEnd.endsWith(aEnd))                return true;        }        return false;    }

4、通過調用OpenFiles類返回的Intent,開啟相應的檔案


if(currentPath!=null&¤tPath.isFile())                {                    String fileName = currentPath.toString();                    Intent intent;                    if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingImage))){                        intent = OpenFiles.getImageFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingWebText))){                        intent = OpenFiles.getHtmlFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingPackage))){                        intent = OpenFiles.getApkFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingAudio))){                        intent = OpenFiles.getAudioFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingVideo))){                        intent = OpenFiles.getVideoFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingText))){                        intent = OpenFiles.getTextFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingPdf))){                        intent = OpenFiles.getPdfFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingWord))){                        intent = OpenFiles.getWordFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingExcel))){                        intent = OpenFiles.getExcelFileIntent(currentPath);                        startActivity(intent);                    }else if(checkEndsWithInStringArray(fileName, getResources().                            getStringArray(R.array.fileEndingPPT))){                        intent = OpenFiles.getPPTFileIntent(currentPath);                        startActivity(intent);                    }else                    {                        showMessage("無法開啟,請安裝相應的軟體!");                    }                }else                {                    showMessage("對不起,這不是檔案!");                }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.