views:

33

answers:

1

I know how to lunch another activity, But how do I meke sure an activity exist for my MIME type before starting the activity? for example if I have a PDF file that I want to display, how do I make sure a PDF viewer exist?

here is the code I use to lunch the PDF viewer

MimeTypeMap tMimeType = MimeTypeMap.getSingleton();
String tMimeStr = tMimeType.getMimeTypeFromExtension("pdf");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile("xyz.pdf"), tMimeStr);
try
{
    startActivity(intent);
}
catch (Exception e)
{
    // Display error message here
}
+1  A: 

Take the Intent and pass it to PackageManager's queryIntentActivities(). If you get a zero-length list back, there is nothing that can handle your Intent. If you get a list with two or more entries, consider using Intent.createChooser() to give the user a choice of what activity to use.

CommonsWare
Worked like a charmThanks
Sam