android - 用intent打开文件问题

查看:122
本文介绍了android - 用intent打开文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我打算用下面这段代码打开一个word文件,

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;
    }

调用的代码是

Intent wordIntent= FileUtil.getWordFileIntent(file);
context.startActivity(wordIntent);
手机中存在打开word文件的app,就可以顺利打开。如果不存在打开的app,程序就会报no Activity found to handle intent错误。请问下是怎么回事?

解决方案

try catch处理一下这个异常即可。

这篇关于android - 用intent打开文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆