How manage “No application can perform this action

2019-07-19 05:49发布

So this is my code

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

Where my Utils.openfile() is

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

Now as you see I'd like to manage that if can't found any app that can opens my file (pdf,mobi or epub) I'd like to start an intent to link to android market and search any apps..

But, I don't know why, my code never runs into exception but just show a dialog that say "No application can perform this action".. How could I manage and achieve my target?

1条回答
疯言疯语
2楼-- · 2019-07-19 06:42

Try getting rid of createChooser(). After all, that's not needed for ACTION_VIEW anyway -- if the user chose a default for this MIME type, please allow them to use it. And, the chooser is preventing you from getting ActivityNotFoundException, because the chooser is the activity, and it always exists.

查看更多
登录 后发表回答