Implementing pdf.js in android to read from sd card

孤者浪人 提交于 2019-12-12 15:27:49

问题


I'm trying to implement a pdf viewer for my android app which displays different pdf files which are present in the SD card.

I am thinking of using the pdf.js library.I used the code sample as posted here: https://bitbucket.org/butelo/pdfviewer/

However, the library takes the pdf url in the javascript file which is relative address to the folder to which it belongs (/assets/pdfviewer).

<script type="text/javascript">
var url = '../compressed.tracemonkey-pldi-09.pdf';
</script>

How can I redirect it to use a pdf present in a folder in the sdcard ?

Also the filenames of the pdfs are not fixed and I need to change them in the program as per requirement.

Update --------------------------------------------------------------------------------------------------------------------------

I updated the java code like this:

Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/data/test.pdf");
        webView.loadUrl("file:///android_asset/pdfviewer/index.html?file=" + path);

In the pdffile.js, I modified the following:

From:

<script type="text/javascript">
var url = '../compressed.tracemonkey-pldi-09.pdf';
</script>

To:

var url = getURLParameter('file');


function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null}

The above javascript code extracts the 'file' parameter from the URL of the 'index.html'

Still does not work. The webview 'chromium' in logcat shows:

I/chromium(1353): [INFO:CONSOLE(106)] "Warning: Unhandled rejection: 
Unexpected server response (0) while retrieving PDF "file:///storage/sdcard0/data/test.pdf".", source: file:///android_asset/pdfviewer/pdf.js (106)"

This seems to be cross server issue. So how can I modify the pdf.js code to read local files without server ?


回答1:


Solved the issue. The app was missing READ_EXTERNAL_STORAGE permissions.

The code can be found at: https://github.com/pauldmps/Android-pdf.js Its Apache V2 license, so feel free to use in your app.



来源:https://stackoverflow.com/questions/24291980/implementing-pdf-js-in-android-to-read-from-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!