在Android中显示PDF

前端之家收集整理的这篇文章主要介绍了在Android中显示PDF前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的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 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);

解决方法

将手机连接到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读者的约束.但是你可以先尝试一个隐含的意图,如果失败则尝试将显式意图作为后备.

原文链接:https://www.f2er.com/android/316046.html

猜你在找的Android相关文章