編輯:Android開發實例
本文實例講述了Android編程開發之打開文件的Intent及使用方法。分享給大家供大家參考,具體如下:
在寫文件管理系統時會用到各種打開不同格式的文件的需求,由於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>.p<?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>df</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("對不起,這不是文件!"); }
希望本文所述對大家Android程序設計有所幫助。
Android的控件有很多種,其中就有一個Spinner的控件,這個控件其實就是一個下拉顯示列表。Spinner是位於 android.widget包
在android開發中ListView是比較常用的組件,它以列表的形式展示具體內容,並且能夠根據數據的長度自適應顯示。抽空把對ListView的使用做了整理,
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
JSON代表JavaScript對象符號。它是一個獨立的數據交換格式,是XML的最佳替代品。本章介紹了如何解析JSON文件,並從中提取所需的信息。Android提供了四個