开发者

Findout if a default viewer exist

开发者 https://www.devze.com 2023-01-05 15:26 出处:网络
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 sur

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

精彩评论

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

关注公众号