android中pdf,在Android中显示PDF

博客内容讨论了在Android应用中如何检查设备是否存在PDF查看器。作者在onCreate()方法中尝试通过意图 Intent.ACTION_VIEW 来寻找能够处理PDF类型的活动,但在HTC Desire手机上即使安装了Adobe PDF查看器,也无法找到匹配项。问题在于Adobe可能没有公开的意图,导致查询失败。文章询问是否应该直接启动Acrobat或是否存在其他PDF查看器解决方案,特别是在下载并保存PDF文件后如何打开它。

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

在我的onCreate()中我做了这个检查:

//

// check if we have a PDF viewer,else bad things happen

//

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setType("application/pdf");

List intents = getPackageManager().queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);

if (intents == null || intents.size() == 0) {

// display message then...

finish();

}

在我的HTC Desire上,即使我有Adobe的PDF查看器,也不会返回匹配项.这个问题的答案android: open a pdf from my app using the built in pdf viewer提到Adobe可能没有任何公共意图,因此上述检查显然不会返回任何内容.

任何人都可以验证您是否应该从意图启动Acrobat,或者是否有其他方法或PDF查看器可供使用.

实际用例是下载发票副本并使用以下代码将其存储在本地存储中:

URL url = new URL(data);

InputStream myInput = url.openConnection().getInputStream();

FileOutputStream fos = openFileOutput(fname,Context.MODE_WORLD_READABLE);

// transfer bytes from the input file to the output file

byte[] buffer = new byte[8192];

int length;

while ((length = myInput.read(buffer)) > 0) {

fos.write(buffer,length);

progressDialog.setProgress(i++);

}

fos.close();

然后展示

// read from disk,and call intent

openFileInput(fname); // will throw FileNotFoundException

File dir = getFilesDir(); // where files are stored

File file = new File(dir,fname); // new file with our name

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.fromFile(file));

intent.setType("application/pdf");

startActivity(intent);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值