How manage "No application can perform this action" (AKA ActivityNotFoundException)

3.9k views Asked by At

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

There are 1 answers

0
CommonsWare On BEST ANSWER

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.