看了几个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/