android - 打开并显示本地存储的 pdf 文件

标签 android pdf local-storage file-management

看了几个Q/A,还是找不到适合我当前问题的答案。

我有一个 pdf 文件(在编译时已知)存储在我的/res/raw 文件夹中。

我尝试使用以下方式加载文件:

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

然后我想在设备上使用首选的 pdf 阅读器显示 pdf( Intent ):

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

问题是 Intent 采用"file"类型,而我的 pdf 被读取为“InputStream”。

问题是:如何显示 pdf 文件?即如何显示 InputStream?或者我如何存储 pdf 文件以允许使用 new File() 打开?

最佳答案

试试这个..
//将pdf放在 Assets 文件夹中只是为了尝试

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

关于android - 打开并显示本地存储的 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10123227/

相关文章:

java - 在android中的onCreate方法内部调用onCreate方法

image - iTextSharp 水平多页超宽图像

angularjs - 应用程序卸载时未清除LocalStorage-Android 6

android - Cordova 插件 - Javascript 从 native 代码接收消息

android - 使用 setScaleX() setScaleY() 更改 android 窗口大小

java - Android Studio - 模糊的背景图像

macos - Mac Automator - 合并 PDF 文件,保存在同一文件夹中

pdf - PyPDF2 : Error -5 while decompressing data: incomplete or truncated stream

javascript - onClick 将图像存储在 HTML5 本地存储中

javascript - 如何为同一来源的 iframe 设置单独的 sessionStorages