打开并显示PDF文件存储在本地

打开并显示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();
                }