2

所以这是我的代码

    public void onClick() {
                try {
                     startActivity(Utils.openFile(f.getPath(),myExt));
                }
                catch(ActivityNotFoundException activityNotFoundException) {
                    Toast.makeText(mContext, "Nessuna App trovata", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("market://search?q="+myExt+"&c=apps"));
                    startActivity(intent);
                }   

我的 Utils.openfile() 在哪里

public static Intent openFile(String u,String b) {
    File file = new File(u);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file),"application/" + b);
    target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent = Intent.createChooser(target, "Apri File");
    return intent;       
}

现在如您所见,我想管理如果找不到任何可以打开我的文件(pdf、mobi 或 epub)的应用程序,我想开始一个意图链接到 android 市场并搜索任何应用程序..

但是,我不知道为什么,我的代码永远不会遇到异常,只是显示一个对话框,上面写着“没有应用程序可以执行此操作”。我该如何管理和实现我的目标?

4

1 回答 1

3

尝试摆脱createChooser(). 毕竟,这ACTION_VIEW无论如何都不需要——如果用户为此 MIME 类型选择了默认值,请允许他们使用它。而且,选择器会阻止您获得ActivityNotFoundException,因为选择器是活动,并且始终存在。

于 2013-08-08T12:55:59.033 回答