android intent打开各种文件(txt、apk、ppt、xls和doc)的方法,下面有代码 直接运行

switch (v.getId()){

case R.id.button_txt:

filePath = “/storage/emulated/0/hello2.txt”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

case R.id.button_apk:

filePath = “/storage/emulated/0/app-debug.apk”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

case R.id.button_ppt:

filePath = “/storage/emulated/0/新建 PPT 演示文稿.ppt”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

case R.id.button_xls:

filePath = “/storage/emulated/0/新建 XLS 工作表.xls”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

case R.id.button_doc:

filePath = “/storage/emulated/0/新建 DOC 文档.doc”;

intent = FileUtils.openFile(this,filePath);

startActivity(intent);

break;

}

}

}

2.FileUtils.java中:

public class FileUtils {

//打开类型的文件

public static Intent openFile(Context context, String filePath) {

if (filePath == null) {

return null;

}

File file = new File(filePath);

if (!file.exists())

return null;

/* 取得扩展名 */

String end = file

.getName()

.substring(file.getName().lastIndexOf(“.”) + 1,

file.getName().length()).toLowerCase();

end = end.trim().toLowerCase();

// System.out.println(end);

/* 依扩展名的类型决定MimeType */

if (end.equals(“apk”)) {

return getApkFileIntent(context,filePath);

} else if (end.equals(“ppt”)) {

return getPptFileIntent(context,filePath);

  • 26
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值