查看PDF格式的Andr​​oid [英] Show PDF in Android

查看:114
本文介绍了查看PDF格式的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的onCreate()我这样做检查:

  //
//检查,如果我们有一个PDF阅读器,其他不好的事情发生
//
意向意图=新的意图(Intent.ACTION_VIEW);
intent.setType(应用程序/ PDF格式);

名单< ResolveInfo>意图= getPackageManager()queryIntentActivities(意向,PackageManager.MATCH_DEFAULT_ONLY)。

如果(意图== NULL || intents.size()== 0){
       //显示信息,那么...
       完();
}
 

在我的HTC Desire,这不返回的比赛,即使我有Adobe的PDF阅读器。回答这个问题的<一个href="http://stackoverflow.com/questions/2916108/android-open-a-pdf-from-my-app-using-the-built-in-pdf-viewer">http://stackoverflow.com/questions/2916108/android-open-a-pdf-from-my-app-using-the-built-in-pdf-viewer提到Adobe可能不会有任何公开的意图,因此上述检查显然返回任何结果。

任何人都可以验证您是否应该能够推出Acrobat和意图,或者是有一些其他的方法或PDF浏览器使用。

实际使用情况是下载的发票复印件,并使用code,如将它们存储在本地存储:

 网​​址URL =新的URL(数据);
 InputStream的myInput = url.openConnection()的getInputStream()。

 FileOutputStream中FOS = openFileOutput(FNAME,Context.MODE_WORLD_READABLE);

 //传输的字节从输入文件到输出文件
 byte []的缓冲区=新的字节[8192];
 INT长;
 而((长度= myInput.read(缓冲液))大于0){
    fos.write(缓冲液,0,长度);
    progressDialog.setProgress(我+ +);
 }
 fos.close();
 

然后显示

  //从磁盘中读取,并呼吁意图
openFileInput(FNAME); //将抛出FileNotFoundException异常

文件DIR = getFilesDir(); //文件的存储位置
档案文件=新的文件(目录,FNAME); //用我们的名字的新文件

意向意图=新的意图(Intent.ACTION_VIEW,Uri.fromFile(文件));
intent.setType(应用程序/ PDF格式);

startActivity(意向);
 

解决方案

连接手机到你的PC,启动Eclipse并打开LogCat中。然后下载一个PDF文件浏览器,并打开它。你应该看到一个线,如(我用的是HTC渴望):

09-14 17:45:58.152:信息/ ActivityManager(79):启动活动:意向{行为= android.intent.action.VIEW DAT =文件:///sdcard/download/FILENAME.pdf典型值=应用/ PDF FLG = 0x4000000 CMP = com.htc.pdfreader / .ActPDFReader}

与使用该组件信息的明确意图一展身手。文档在这里说:

  

>   组件 - 指定组件类用于意图的一个明确的名称。通常这是通过查看在意图(动作,数据/类型和类别)的其他信息和匹配,与一个组件,可以处理它确定。如果该属性被设置,那么没有评价的被执行,并且该组件被用来精确地原样。通过指定这个属性,所有其他的意向属性变为可选。

坏处就是你会被绑定到HTC读者。但是,你可以先尝试一个隐含的意图,如果失败尝试的明确意图作为备用。

In my onCreate() I do this check:

//
// 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();
}

On my HTC Desire, this doesn't return a match, even though I have Adobe's PDF viewer. An answer to this question http://stackoverflow.com/questions/2916108/android-open-a-pdf-from-my-app-using-the-built-in-pdf-viewer mentions that Adobe may not have any public Intents, so the above check will obviously return nothing.

Can anyone verify whether you should be able launch Acrobat from an intent, or is there some other method or PDF viewer to use.

The actual use case is downloading copies of invoices and storing them on local storage using code such as:

 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, 0, length);
    progressDialog.setProgress(i++);
 }
 fos.close();

and then to show

// 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);

解决方案

Connect your phone to you PC, start Eclipse and open the LogCat. Then download a PDF file with the browser and open it. You should see a line such as (I used the HTC desire):

09-14 17:45:58.152: INFO/ActivityManager(79): Starting activity: Intent { act=android.intent.action.VIEW dat=file:///sdcard/download/FILENAME.pdf typ=application/pdf flg=0x4000000 cmp=com.htc.pdfreader/.ActPDFReader }

Have a go with an explicit intent using the component information. Docs say here:

> 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.

Downside is you will be bound to the htc reader. But you could try an implicit intent first and if that fails try the explicit intent as a fallback.

这篇关于查看PDF格式的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆