在Android中显示PDF

在Android中显示PDF,第1张

概述在我的onCreate()中我做了这个检查: //// check if we have a PDF viewer, else bad things happen//Intent intent = new Intent(Intent.ACTION_VIEW);intent.setType("application/pdf");List<ResolveInfo> intents = ge 在我的onCreate()中我做了这个检查:
//// check if we have a pdf vIEwer,else bad things happen//Intent intent = new Intent(Intent.ACTION_VIEW);intent.setType("application/pdf");List<ResolveInfo> 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 intentopenfileinput(fname);   // will throw fileNotFoundExceptionfile dir = getfilesDir();       // where files are storedfile file = new file(dir,fname);   // new file with our nameIntent intent = new Intent(Intent.ACTION_VIEW,Uri.fromfile(file));intent.setType("application/pdf");startActivity(intent);
解决方法 将手机连接到PC,启动Eclipse并打开LogCat.然后使用浏览器下载pdf文件并将其打开.你应该看到一条线(我使用了HTC的愿望):

09-14 17:45:58.152:INFO / ActivityManager(79):启动活动:Intent {act = androID.intent.action.VIEW dat = file:///sdcard/download/filename.pdf typ = application / pdf flg = 0x4000000 cmp = com.htc.pdfreader / .ActpdfReader}

使用组件信息明确意图.文档在这里说:

>
component — SpecifIEs an explicit name of a component class to use for the intent. normally this is determined by looking at the other information in the intent (the action,data/type,and categorIEs) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed,and this component is used exactly as is. By specifying this attribute,all of the other Intent attributes become optional.

下行是你将受到htc读者的约束.但是你可以先尝试一个隐含的意图,如果失败则尝试将显式意图作为后备.

总结

以上是内存溢出为你收集整理的在Android中显示PDF全部内容,希望文章能够帮你解决在Android中显示PDF所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1136572.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存