android查看手机是否安装了打开某类附件的第三方应有 并用其打开附件
来源:互联网 发布:mac永恒战士2无限钻石 编辑:程序博客网 时间:2024/06/05 03:59
移动开发中总要遇到一些各类附件的查看问题,ios可以通过系统的机制用浏览器打开,android我们可以通过安装第三方的应用打开附件(如用wps打开word excel等等)
现在我们来介绍一下android中的实现
查看所有能够打开某个附件的应用的方法
PackageManager pManager = this.getPackageManager();//this 是contextList list = pManager.queryIntentActivities(intent, PackageManager.GET_ACTIVITIES);
这个List就是获得 能够打开某类附件的应用的相关信息
可以看到这里面需一个intent 实现如下;
public class OnAttachmentOpenedActions { public static Intent getAttachmentIntent(File file, String filetype){ filetype = filetype.toLowerCase(); if(filetype.equals("doc")||filetype.equals("docx")){ return getWordFileIntent(file) ; } else if(filetype.equals("pdf")){ return getPdfFileIntent(file) ; } else if(filetype.equals("image")){ return getImageFileIntent(file) ; } else if(filetype.equals("jpg")||filetype.equals("gif")||filetype.equals("png")|| filetype.equals("jpeg")||filetype.equals("bmp")){ return getImageFileIntent(file) ; } else if(filetype.equals("txt")){ return getTextFileIntent(file) ; } else if(filetype.equals("xls")||filetype.equals("xlsx")){ return getExcelFileIntent(file) ; } else if(filetype.equals("ppt")||filetype.equals("pptx")){ return getPptFileIntent(file) ; } else if(filetype.equals("html")||filetype.equals("htm")){ return getHtmlFileIntent(file) ; } return getDefaultFileIntent(file); }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;}public static Intent getHtmlFileIntent(String content) {Uri uri = Uri.parse(content).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(content).build();Intent intent = new Intent("android.intent.action.VIEW");intent.setDataAndType(uri, "text/html");return 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 uri2 = Uri.fromFile(file);intent.setDataAndType(uri2, "text/plain");return 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( 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; } public static Intent getPdfFileIntent(File file) {Uri path = Uri.fromFile(file);Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(path, "application/pdf");intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);return intent;} //Android获取一个用于打开Excel文件的intent public static Intent getExcelFileIntent( File file ){ Uri path = Uri.fromFile(file); Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(path, "application/vnd.ms-excel"); return intent; } //Android获取一个用于打开PPT文件的intent public static Intent getPptFileIntent(File file ){ Uri path = Uri.fromFile(file); Intent intent = new Intent("android.intent.action.VIEW"); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(path, "application/vnd.ms-powerpoint"); return intent; } private 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; } public static Intent getDefaultFileIntent(File file) {Intent intent = new Intent("android.intent.action.VIEW");intent.addCategory("android.intent.category.DEFAULT");intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);Uri uri2 = Uri.fromFile(file);intent.setDataAndType(uri2, "");return intent;}}
这样就可以通过list的判断是否安装应用 打开就用startActivity(intent)就可以了。
0 0
- android查看手机是否安装了打开某类附件的第三方应有 并用其打开附件
- android调用第三方软件打开下载的附件
- android调用第三方软件打开下载的附件
- android 打开附件
- 选择手机上软件打开附件
- android 第三方应用读取彩信附件
- Evolution 打开 eml附件
- iOS 附件打开
- android7.0 打开附件
- 打开 Outlook 禁用附件的方法
- oulook无法打开exe附件的解决
- outlook不能打开个别excel的附件
- outlook 附件保存位置-附件无法打开
- Android 打开第三方应用的方法
- Android 第三方应用的打开
- 是开了房间了喀什的肌肤了撒打开附件离开
- Android通过Intent判断程序是否安装及通过第三方打开
- Android-----代码实现打开手机第三方应用APP
- iOS 几种初始化的分析比较
- 手机端解决onkeyup事件
- 中介者模式
- iOS崩溃调试的使用和技巧
- Selenium-Grid 实现案列
- android查看手机是否安装了打开某类附件的第三方应有 并用其打开附件
- Socket入门——TCP协议即时聊天
- 搜狐专家与您一起分享“从人力资源到人力资产”
- Ubuntu14.04 lTS 换源
- HDU 5528 Count a * b(积性函数)
- ESB开发笔记(2)
- Linux 同步时间
- 看图
- 兔子-系统的纯净版,装机版,深度和GHOST的区别