Android打开各种类型的文件方法总结
很简单,通过调用系统的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打开各种类型的文件方法总结的更多相关文章
- Android 各种MIME类型和文件类型的匹配表
MIME:全称Multipurpose Internet Mail Extensions,多功能Internet 邮件扩充服务.它是一种多用途网际邮件扩充协议,在1992年最早应用于电子邮件系统,但后 ...
- Intent MIME 打开各种类型的文件
使用 public class MainActivity extends ListActivity { public static final String path = Environmen ...
- Android || IOS录制mp3语音文件方法
Android Android Supported Media Formats : http://developer.android.com/guide/appendix/media-formats. ...
- 高速在MyEclipse中打开jsp类型的文件
MyEclipse打开jsp时老是要等上好几秒,嗯嗯,这个问题的确非常烦人,事实上都是MyEclipse的"自作聪明"的结果(它默认用Visual Designer来打开的),进行 ...
- Linux-各种姿势(less\vi等)打开各种类型的文件(txt/csv/xlsx等)出现不能打开(全乱码、部分乱码、二进制文件等)的问题
(一)linux各种中文乱码解决办法整理 远程登录服务器用vim在终端下编辑查看文件经常会遇见各种中文乱码问题. 做如下设置可基本解决vim中文乱码问题,首先查看系统对中文的支持locale -a | ...
- 用Linux命令行实现删除和复制指定类型的文件
(一)Linux 删除当前目录及子目录中所有某种类型的文件 方法1 : 此方法不能处理目录中带空格的那些. rm -rf `find . -name "*.example"` Li ...
- 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
打开后缀.apk的文件.即启动安装程序. //apkFilePath 文件路径 public void installAPK(String apkFilePath) { // 创建URI Uri ur ...
- Android 打开方式选定后默认了改不回来?解决方法(三星s7为例)
Android 打开方式选定后默认了改不回来?解决方法(三星s7为例) 刚刚在测试东西,打开一个gif图,然后我故意选择用支付宝打开,然后...支付宝当然不支持,我觉得第二次打开它应该还会问我,没想到 ...
- android 打开各种文件(setDataAndType)转:
android 打开各种文件(setDataAndType) 博客分类: android-->非界面 android 打开各种文件 setDataAndType action动作 转自:htt ...
随机推荐
- springboot的常见问题错误
一: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 2 ...
- hihocoder1524
题目链接 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个1-N的排列A1, A2, ... AN,如果Ai和Aj满足i < j且Ai > Aj,我们 ...
- Vue(十五)组件
一. 组件component 1. 什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码 组件是自定义元素(对象) 2. 定义组件 ...
- zepto 选中select option 的值
1,网上搜的,感觉蛮好用的,先存着 $('#sel').find('option').not(function() {return !this.selected;}).val();
- 使用JfreeChart生成图表遇到的问题
生成的图片不显示 需要在web.xml中配置一个指定的Servlet <servlet> <servlet-name>DisplayChart</servlet-name ...
- fiddler抓取手机上https数据失败,全部显示“Tunnel to......443”解决办法
与后端数据通信是前端日常开发的重要一环,在与后端接口联调的时候往往需要通过查看后端返回的数据进行调试.如果在PC端,Chrome自带的DevTools就已经足够用了,Network面板可以记录所有网络 ...
- jeffy-vim-v3.1.tar.gz
下载链接: https://files.cnblogs.com/files/pengdonglin137/jeffy-vim-v3.1.tar.gz 1. 使用sublimemonokai配色 2. ...
- javacript onclick事件中传递对象参数
var user = {id:1, name:'zs', age:20}; var ele = '<a onclick="edit(' + JSON.stringify(user).r ...
- Python中syncio和aiohttp
CPython 解释器本身就不是线程安全的,因此有全局解释器锁(GIL),一次只允许使用一个线程执行 Python 字节码.因此,一个 Python 进程通常不能同时使用多个 CPU 核心.然而,标准 ...
- SharePoint Excel Service - Couldn't Open the Workbook.
Error meesage: "Couldn't Open the Workbook. Wow, That's a big workbook. Unfortunately, we can't ...