Android 打开pdf文档,没有阅读器链接到Google Play Store等下载

本文介绍如何在Android设备上使用默认PDF阅读器打开文档,并提供没有阅读器时的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用自带的pdf阅读器,打开pdf文档。如果没有阅读器,链接到android市场(Google Play Store,360手机助手等)下载。

File file = new File(pdfPath);
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    Intent marketIntent = new Intent(Intent.ACTION_VIEW);
    marketIntent.setData(Uri.parse("market://search?q=PDF"));
    startActivity(marketIntent);
}

必须加try{}catch{}, 否则,如果没有阅读器,程序会崩溃报错ActivityNotFoundException。

想要了解更多market://的知识,请点击http://blog.youkuaiyun.com/liucaoye/article/details/41449059

想要了解各种类型文件(“.html”,".doc",".mp3"上百种类型)对应的setDataAndType的参数,参考 http://www.2cto.com/kf/201201/117389.html

想要了解Intent的各种跳转(email,短信,map,电话等),参考http://luhuajcdd.iteye.com/blog/1560225


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值