I have built a small app to render the pdf file (e.g. income_tax_return.pdf) with the PDF.js which works fine. The PDF file is inside the assests direcotry of the Android project as well as the viewer.html. Now I am trying to load the PDF from the download directory of my Andriod device and adding it as a String to the
?file
attribute in the loadUrl() method.
How can I add the path to the file attribute in the loadUrl() to render the PDF?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Use local resource
Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf");
Log.i("ABCD",Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf"); //output: /storage/emulated/0/income_tax_return.pdf
mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=income_tax_return.pdf");
}
Edit
when replacing my code with:
String path = Environment.getExternalStorageDirectory().toString() + "/Download/income_tax_return.pdf";
mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=" + path);
I am getting:
PDF.js Version 1.8.170 (build: 3ca67550 ) Missing PDF "file:///android_asset/psdfjs/web/Download/ income_tax_return.pdf"