很简单,通过调用系统的intent,我们可以打开各种文件,不熟悉的朋友可以了解下action、datatype、uri的相关知识。

通用方法如下:

    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文件的intent
public 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文件的intent
public 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文件的intent
public 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文件的intent
public 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获取一个用于打开图片文件的intent
public 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;
}

来自:http://blog.csdn.net/eclipsexys/article/details/39552501

Android打开各种类型的文件方法总结的更多相关文章

  1. Android 各种MIME类型和文件类型的匹配表

    MIME:全称Multipurpose Internet Mail Extensions,多功能Internet 邮件扩充服务.它是一种多用途网际邮件扩充协议,在1992年最早应用于电子邮件系统,但后 ...

  2. Intent MIME 打开各种类型的文件

    使用 public class MainActivity extends ListActivity {     public static final String path = Environmen ...

  3. Android || IOS录制mp3语音文件方法

    Android Android Supported Media Formats : http://developer.android.com/guide/appendix/media-formats. ...

  4. 高速在MyEclipse中打开jsp类型的文件

    MyEclipse打开jsp时老是要等上好几秒,嗯嗯,这个问题的确非常烦人,事实上都是MyEclipse的"自作聪明"的结果(它默认用Visual Designer来打开的),进行 ...

  5. Linux-各种姿势(less\vi等)打开各种类型的文件(txt/csv/xlsx等)出现不能打开(全乱码、部分乱码、二进制文件等)的问题

    (一)linux各种中文乱码解决办法整理 远程登录服务器用vim在终端下编辑查看文件经常会遇见各种中文乱码问题. 做如下设置可基本解决vim中文乱码问题,首先查看系统对中文的支持locale -a | ...

  6. 用Linux命令行实现删除和复制指定类型的文件

    (一)Linux 删除当前目录及子目录中所有某种类型的文件 方法1 : 此方法不能处理目录中带空格的那些. rm -rf `find . -name "*.example"` Li ...

  7. 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)

    打开后缀.apk的文件.即启动安装程序. //apkFilePath 文件路径 public void installAPK(String apkFilePath) { // 创建URI Uri ur ...

  8. Android 打开方式选定后默认了改不回来?解决方法(三星s7为例)

    Android 打开方式选定后默认了改不回来?解决方法(三星s7为例) 刚刚在测试东西,打开一个gif图,然后我故意选择用支付宝打开,然后...支付宝当然不支持,我觉得第二次打开它应该还会问我,没想到 ...

  9. android 打开各种文件(setDataAndType)转:

    android 打开各种文件(setDataAndType) 博客分类: android-->非界面 android 打开各种文件 setDataAndType action动作  转自:htt ...

随机推荐

  1. JS基本数据类型

    基本数据类型: Undefined,null,boolean,number,string symbol(ES6) 复杂数据类型: object undefined: 变量声明未初始化,自动为undef ...

  2. Flask 三方组件 WTForms

    WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证 from flask import Blueprint from flask import request fro ...

  3. 02-CSS&JS

    今日目标 使用CSS完成网站首页的优化 使用CSS完成网站注册页面的优化 使用JS完成简单的数据校验 使用JS完成图片轮播效果 教学目标: - 了解CSS的概念 - 了解CSS的引入方式 - 了解CS ...

  4. kindeditor富文本编辑器初步使用教程

    下载kindeditor 可以选择去官网下载(http://kindeditor.net/down.php),不过要FQ:或者直接CSDNhttp://download.csdn.net/downlo ...

  5. Python ----pip安装模块提示“unknown or unsupported command install”的解决办法

    安装pip后,使用pip安装模块时,提示“unknown or unsupported command install” 解决方法: 1.cmd运行"where pip" 找出所有 ...

  6. 深入理解javascript构造函数和原型对象

    ---恢复内容开始--- 对象,是javascript中非常重要的一个梗,是否能透彻的理解它直接关系到你对整个javascript体系的基础理解,说白了,javascript就是一群对象在搅..(哔! ...

  7. JQ03

    JQ03 1.val方法 val方法用于设置和获取表单元素的值,如input/textarea 1)设置与获取: .val("需要设置的字符串"): .val();//获取字符串 ...

  8. Android开发中常见的设计模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. 对Faster R-CNN的理解(2)

    2. 区域建议网络 区域建议网络(Regional Proposal Network, RPN),根据特征图上每一个点的向量,为这个点生成k个矩形建议框.每一个点输出的内容包括:reg层4个输出x.y ...

  10. 单片机 MCU 中 stack 使用的探讨

    stack 的使用,是单片机开发中影响最大,但是最少被讨论的问题.而提及这个问题的地方,都是对这个问题含糊其辞. 今天花了点时间,使用最笨的办法,直接阅读汇编代码,来对这个问题就行探究,这里做一下记录 ...