开发者

Display PDF in webview

I want to show a pdf in my application. My webservice gives me the pdf and I want to download it to sd card and then I want show tha开发者_Python百科t pdf in my app through webview.


I was also struggling with Android PDF showing problem for some time. I did the same approach as you mentioned with downloading the PDF to the SD card, but I did not manage to open it up otherwise than using a preinstalled Android App which could do the PDF opening (e.g. Adobe Reader or similar). I think that it's not possible to view the PDF in a WebView widget. The reason is simply that the included webview does not support plugins like adobe reader (see: http://osdir.com/ml/Android-Developers/2010-09/msg03331.html)

You can easily open up the downloaded PDF using an Intent call:

   File file = new File("/sdcard/filename.pdf");
   Uri path = Uri.fromFile(file);
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(path, "application/pdf");
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
   startActivity(intent);

Due to the fact that this technique opens up the PDF viewer directly after my application makes this call, the back button takes the user directly back to the app, so it feels like the pdf viewing is part of my app.

Hope this helps, best regards


As already answered here, the WebView can't render PDFs. You can do what it is said in the accepted answer in this question and try to open the PDF with the default PDF viewer in the device. If the device has no PDF viewer installed, a message will be displayed to the user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜