开发者

Findout if a default viewer exist

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开发者_C百科 here
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜