打开并显示PDF文件存储在本地 [英] Open and display pdf-file stored locally

查看:269
本文介绍了打开并显示PDF文件存储在本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过几个Q / A看完后,我仍然无法找到我的本期合适的答案。

After reading through several Q/A, I still can't find a suitable answer for my current issue.

我有存储在我的/ RES /原始文件夹中的PDF文件(在编译时已知)。

I have a pdf-file (known at compile time) which is stored in my /res/raw folder.

我曾尝试使用加载文件:

I have tried loading the file using:

InputStream is = getResources().openRawResource(R.raw.mypdf);

然后我想用preferred PDF阅读器的设备上显示PDF(在意向):

Then I want to display the pdf (in an intent) using the preferred pdf-reader on the device:

Intent i;
i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(file,"application/pdf");
startActivity(i);

问题是,意图发生在类型'文件',而我的PDF阅读作为一个'的InputStream。

The issue is that the intent takes in the type 'File', while my pdf is read as an 'InputStream'.

现在的问题是:我怎样才能显示PDF文件?即我怎么能显示一个InputStream?或者我怎么能存储的PDF文件,以便与新的文件打开()?

The question is: How can i display the pdf-file? i.e. how can I display an InputStream? or how can I store the pdf-file to allow opening with new File()?

推荐答案

试试这个..结果
    //在资源文件夹位置PDF只是尝试

TRY this..
//place pdf in asset folder just to try

Uri file= Uri.parse("file:///android_asset/mypdf.pdf");
  String mimeType =  MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));

try{
     Intent i;
     i = new Intent(Intent.ACTION_VIEW);
     i.setDataAndType(file,mimeType);
     startActivity(i);

}catch (ActivityNotFoundException e) {
                    Toast.makeText(this, 
                        "No Application Available to fiew this file type", 
                        Toast.LENGTH_SHORT).show();
                } 

这篇关于打开并显示PDF文件存储在本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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