Android 系统默认打开文件的方法
来源:互联网 发布:阿里云 百度云 知乎 编辑:程序博客网 时间:2024/06/10 00:13
Android 系统默认打开文件的方法:
public static Intent openFile(String filePath){File file = new File(filePath);if(!file.exists()) return null;/* 取得扩展名 */String end=file.getName().substring(file.getName().lastIndexOf(".") + 1,file.getName().length()).toLowerCase(); /* 依扩展名的类型决定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获取一个用于打开APK文件的intentpublic static Intent getAllIntent( String param ) {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获取一个用于打开APK文件的intentpublic static Intent getApkFileIntent( String param ) {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获取一个用于打开VIDEO文件的intentpublic static Intent getVideoFileIntent( String param ) {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获取一个用于打开AUDIO文件的intentpublic static Intent getAudioFileIntent( String param ){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获取一个用于打开Html文件的intent public static Intent getHtmlFileIntent( String param ){Uri uri = Uri.parse(param ).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(param ).build();Intent intent = new Intent("android.intent.action.VIEW");intent.setDataAndType(uri, "text/html");return intent;}//Android获取一个用于打开图片文件的intentpublic static Intent getImageFileIntent( 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, "image/*");return intent;}//Android获取一个用于打开PPT文件的intent public static Intent getPptFileIntent( 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/vnd.ms-powerpoint"); return intent; } //Android获取一个用于打开Excel文件的intent public static Intent getExcelFileIntent( 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/vnd.ms-excel"); return intent; } //Android获取一个用于打开Word文件的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获取一个用于打开CHM文件的intent public static Intent getChmFileIntent( 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/x-chm"); return intent; } //Android获取一个用于打开文本文件的intent public static Intent getTextFileIntent( String param, boolean paramBoolean){ 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获取一个用于打开PDF文件的intent public static Intent getPdfFileIntent( 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/pdf"); return intent; }
阅读全文
0 0
- Android 系统默认打开文件的方法
- 使用系统默认的程序打开文件
- 改变myeclipse默认打开文件的方法
- Mac OS X系统上改变文件默认打开方式的方法
- window7取消文件默认打开方式的方法
- 使用intent用默认的程序打开文件Android代码
- WindowForms调用系统默认程序打开文件
- 系统文件默认打开方式锁定
- Android调用默认浏览器打开指定Url的方法实例
- Android打开系统文件管理器的坑
- android打开系统相册的一些方法
- android 打开系统图库的方法
- WIN32,,打开文件没有默认程序,调用系统的默认程序选择窗口
- android打开文件方法
- android打开文件方法
- Android打开文件方法
- Android 文件打开方法
- Android系统如何修改默认打开程序
- R Note 统计基础
- java.lang.UnsupportedClassVersionError: org/apache/activemq/spring/ActiveMQConnectionFactory : Unsup
- 通用爬虫(搜索网站爬虫)
- Qt 中如何捕获窗口停用和激活的消息 Activate&Deactivate
- linux 7
- Android 系统默认打开文件的方法
- spring mvc demo案例
- 8051 系列单片机内部结构
- 远程连接mysql报错1130,is not allowed to connect to...
- idea警告:'xxx==false' can be simplified to '!xxx'
- 小程序
- Java中常用数据结构
- Java中使用注解
- SSM入门项目-0-前言