Android开发打开文件的Intent及使用
在写文件管理系统时会用到各种打开不同格式的文件的需求,由于Android系统默认内置了一些可以打开的系统应用,但还是不能满足需求,比如打开视频文 件、word等,需要安装相应的播放软件才可以使用,这时程序会通过Intent查找可以使用的软件实现通过代码打开一个文件需要2部分,一部分是要获取 到不同文件的后缀,以便根据需求匹配相应的Intent,另一个就是不同格式的文件打开的Intent不同1、在values目录下定义后缀数组文件 fileendings
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <array name="fileEndingImage"> 4 <item>.png</item> 5 <item>.gif</item> 6 <item>.jpg</item> 7 <item>.jpeg</item> 8 <item>.bmp</item> 9 </array> 10 <array name="fileEndingAudio"> 11 <item>.mp3</item> 12 <item>.wav</item> 13 <item>.ogg</item> 14 <item>.midi</item> 15 </array> 16 <array name="fileEndingVideo"> 17 <item>.mp4</item> 18 <item>.rmvb</item> 19 <item>.avi</item> 20 <item>.flv</item> 21 </array> 22 <array name="fileEndingPackage"> 23 <item>.jar</item> 24 <item>.zip</item> 25 <item>.rar</item> 26 <item>.gz</item> 27 <item>.apk</item> 28 <item>.img</item> 29 </array> 30 <array name="fileEndingWebText"> 31 <item>.htm</item> 32 <item>.html</item> 33 <item>.php</item> 34 <item>.jsp</item> 35 </array> 36 <array name="fileEndingText"> 37 <item>.txt</item> 38 <item>.java</item> 39 <item>.c</item> 40 <item>.cpp</item> 41 <item>.py</item> 42 <item>.xml</item> 43 <item>.json</item> 44 <item>.log</item> 45 </array> 46 <array name="fileEndingWord"> 47 <item>.doc</item> 48 <item>.docx</item> 49 </array> 50 <array name="fileEndingExcel"> 51 <item>.xls</item> 52 <item>.xlsx</item> 53 </array> 54 <array name="fileEndingPPT"> 55 <item>.ppt</item> 56 <item>.pptx</item> 57 </array> 58 59 <array name="fileEndingPdf"> 60 <item>.p<?xml version="1.0" encoding="utf-8"?> 61 <resources> 62 <array name="fileEndingImage"> 63 <item>.png</item> 64 <item>.gif</item> 65 <item>.jpg</item> 66 <item>.jpeg</item> 67 <item>.bmp</item> 68 </array> 69 <array name="fileEndingAudio"> 70 <item>.mp3</item> 71 <item>.wav</item> 72 <item>.ogg</item> 73 <item>.midi</item> 74 </array> 75 <array name="fileEndingVideo"> 76 <item>.mp4</item> 77 <item>.rmvb</item> 78 <item>.avi</item> 79 <item>.flv</item> 80 </array> 81 <array name="fileEndingPackage"> 82 <item>.jar</item> 83 <item>.zip</item> 84 <item>.rar</item> 85 <item>.gz</item> 86 <item>.apk</item> 87 <item>.img</item> 88 </array> 89 <array name="fileEndingWebText"> 90 <item>.htm</item> 91 <item>.html</item> 92 <item>.php</item> 93 <item>.jsp</item> 94 </array> 95 <array name="fileEndingText"> 96 <item>.txt</item> 97 <item>.java</item> 98 <item>.c</item> 99 <item>.cpp</item> 100 <item>.py</item> 101 <item>.xml</item> 102 <item>.json</item> 103 <item>.log</item> 104 </array> 105 <array name="fileEndingWord"> 106 <item>.doc</item> 107 <item>.docx</item> 108 </array> 109 <array name="fileEndingExcel"> 110 <item>.xls</item> 111 <item>.xlsx</item> 112 </array> 113 <array name="fileEndingPPT"> 114 <item>.ppt</item> 115 <item>.pptx</item> 116 </array> 117 118 <array name="fileEndingPdf"> 119 <item>.pdf</item> 120 </array> 121 </resources>df</item> 122 </array> 123 </resources>
2、定义OpenFiles工具类,只需传输File参数即可,然后通过返回的Intent打开文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class OpenFiles { 2 //android获取一个用于打开HTML文件的intent 3 public static Intent getHtmlFileIntent(File file) 4 { 5 Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build(); 6 Intent intent = new Intent("android.intent.action.VIEW"); 7 intent.setDataAndType(uri, "text/html"); 8 return intent; 9 } 10 //android获取一个用于打开图片文件的intent 11 public static Intent getImageFileIntent(File file) 12 { 13 Intent intent = new Intent("android.intent.action.VIEW"); 14 intent.addCategory("android.intent.category.DEFAULT"); 15 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 16 Uri uri = Uri.fromFile(file); 17 intent.setDataAndType(uri, "image/*"); 18 return intent; 19 } 20 //android获取一个用于打开PDF文件的intent 21 public static Intent getPdfFileIntent(File file) 22 { 23 Intent intent = new Intent("android.intent.action.VIEW"); 24 intent.addCategory("android.intent.category.DEFAULT"); 25 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 26 Uri uri = Uri.fromFile(file); 27 intent.setDataAndType(uri, "application/pdf"); 28 return intent; 29 } 30 //android获取一个用于打开文本文件的intent 31 public static Intent getTextFileIntent(File file) 32 { 33 Intent intent = new Intent("android.intent.action.VIEW"); 34 intent.addCategory("android.intent.category.DEFAULT"); 35 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 36 Uri uri = Uri.fromFile(file); 37 intent.setDataAndType(uri, "text/plain"); 38 return intent; 39 } 40 41 //android获取一个用于打开音频文件的intent 42 public static Intent getAudioFileIntent(File file) 43 { 44 Intent intent = new Intent("android.intent.action.VIEW"); 45 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 46 intent.putExtra("oneshot", 0); 47 intent.putExtra("configchange", 0); 48 Uri uri = Uri.fromFile(file); 49 intent.setDataAndType(uri, "audio/*"); 50 return intent; 51 } 52 //android获取一个用于打开视频文件的intent 53 public static Intent getVideoFileIntent(File file) 54 { 55 Intent intent = new Intent("android.intent.action.VIEW"); 56 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 57 intent.putExtra("oneshot", 0); 58 intent.putExtra("configchange", 0); 59 Uri uri = Uri.fromFile(file); 60 intent.setDataAndType(uri, "video/*"); 61 return intent; 62 } 63 64 65 //android获取一个用于打开CHM文件的intent 66 public static Intent getChmFileIntent(File file) 67 { 68 Intent intent = new Intent("android.intent.action.VIEW"); 69 intent.addCategory("android.intent.category.DEFAULT"); 70 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 71 Uri uri = Uri.fromFile(file); 72 intent.setDataAndType(uri, "application/x-chm"); 73 return intent; 74 } 75 76 77 //android获取一个用于打开Word文件的intent 78 public static Intent getWordFileIntent(File file) 79 { 80 Intent intent = new Intent("android.intent.action.VIEW"); 81 intent.addCategory("android.intent.category.DEFAULT"); 82 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 83 Uri uri = Uri.fromFile(file); 84 intent.setDataAndType(uri, "application/msword"); 85 return intent; 86 } 87 //android获取一个用于打开Excel文件的intent 88 public static Intent getExcelFileIntent(File file) 89 { 90 Intent intent = new Intent("android.intent.action.VIEW"); 91 intent.addCategory("android.intent.category.DEFAULT"); 92 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 93 Uri uri = Uri.fromFile(file); 94 intent.setDataAndType(uri, "application/vnd.ms-excel"); 95 return intent; 96 } 97 //android获取一个用于打开PPT文件的intent 98 public static Intent getPPTFileIntent(File file) 99 { 100 Intent intent = new Intent("android.intent.action.VIEW"); 101 intent.addCategory("android.intent.category.DEFAULT"); 102 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 103 Uri uri = Uri.fromFile(file); 104 intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); 105 return intent; 106 } 107 //android获取一个用于打开apk文件的intent 108 public static Intent getApkFileIntent(File file) 109 { 110 Intent intent = new Intent(); 111 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 112 intent.setAction(android.content.Intent.ACTION_VIEW); 113 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); 114 return intent; 115 } 116 117 118 }
3、定义用于检查要打开的文件的后缀是否在遍历后缀数组中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private boolean checkEndsWithInStringArray(String checkItsEnd, 2 String[] fileEndings){ 3 for(String aEnd : fileEndings){ 4 if(checkItsEnd.endsWith(aEnd)) 5 return true; 6 } 7 return false; 8 }
4、通过调用OpenFiles类返回的Intent,打开相应的文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 if(currentPath!=null&¤tPath.isFile()) 2 { 3 String fileName = currentPath.toString(); 4 Intent intent; 5 if(checkEndsWithInStringArray(fileName, getResources(). 6 getStringArray(R.array.fileEndingImage))){ 7 intent = OpenFiles.getImageFileIntent(currentPath); 8 startActivity(intent); 9 }else if(checkEndsWithInStringArray(fileName, getResources(). 10 getStringArray(R.array.fileEndingWebText))){ 11 intent = OpenFiles.getHtmlFileIntent(currentPath); 12 startActivity(intent); 13 }else if(checkEndsWithInStringArray(fileName, getResources(). 14 getStringArray(R.array.fileEndingPackage))){ 15 intent = OpenFiles.getApkFileIntent(currentPath); 16 startActivity(intent); 17 18 }else if(checkEndsWithInStringArray(fileName, getResources(). 19 getStringArray(R.array.fileEndingAudio))){ 20 intent = OpenFiles.getAudioFileIntent(currentPath); 21 startActivity(intent); 22 }else if(checkEndsWithInStringArray(fileName, getResources(). 23 getStringArray(R.array.fileEndingVideo))){ 24 intent = OpenFiles.getVideoFileIntent(currentPath); 25 startActivity(intent); 26 }else if(checkEndsWithInStringArray(fileName, getResources(). 27 getStringArray(R.array.fileEndingText))){ 28 intent = OpenFiles.getTextFileIntent(currentPath); 29 startActivity(intent); 30 }else if(checkEndsWithInStringArray(fileName, getResources(). 31 getStringArray(R.array.fileEndingPdf))){ 32 intent = OpenFiles.getPdfFileIntent(currentPath); 33 startActivity(intent); 34 }else if(checkEndsWithInStringArray(fileName, getResources(). 35 getStringArray(R.array.fileEndingWord))){ 36 intent = OpenFiles.getWordFileIntent(currentPath); 37 startActivity(intent); 38 }else if(checkEndsWithInStringArray(fileName, getResources(). 39 getStringArray(R.array.fileEndingExcel))){ 40 intent = OpenFiles.getExcelFileIntent(currentPath); 41 startActivity(intent); 42 }else if(checkEndsWithInStringArray(fileName, getResources(). 43 getStringArray(R.array.fileEndingPPT))){ 44 intent = OpenFiles.getPPTFileIntent(currentPath); 45 startActivity(intent); 46 }else 47 { 48 showMessage("无法打开,请安装相应的软件!"); 49 } 50 }else 51 { 52 showMessage("对不起,这不是文件!"); 53 }
非著名程序员可能是东半球最好的技术分享公众号。每天,每周定时推送一些有关移动开发的原创文章和教程,微信号:smart_android。