用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”错误。请问下是怎么回事?

图片描述

阅读 4.6k
2 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题