0

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"
TheBook
  • 1,698
  • 1
  • 16
  • 32

3 Answers3

0

Try

String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/income_tax_return.pdf";

mWebView.loadUrl( "file://" + filePath);

how to access downloads folder in android?

Community
  • 1
  • 1
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • with the first code I am getting /storage/emulated/0/income_tax_return.pdf file was not found? The file is in the dowload directory. – TheBook Apr 19 '17 at 09:36
  • Did you add 'Download/' in path? – PEHLAJ Apr 19 '17 at 09:46
  • sorry but this does not work in my case :( please take a look at my second line of the loadUrl() : `mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + path);` – TheBook Apr 19 '17 at 09:56
  • Why do you add 'pdfjs/web/viewer.htm‌​l?file="? – PEHLAJ Apr 19 '17 at 10:04
  • Because I am using PDF.js as PDF viewer it is important in my case. Just like that I can use the pdf.js in the webViewer. – TheBook Apr 19 '17 at 10:07
0
Uri path = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf");
  mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=income_tax_return.pdf");

Change to

Sting path = Environment.getExternalStorageDirectory().toString() + "/income_tax_return.pdf";
mWebView.loadUrl( "file://" + path);

That is not in the Downloads directory of course. But you get the idea.

Edit. Dit you try

mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=Downloads/income_tax_return.pdf");

When that file is in the Downloads directory?

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I added it as the following ` String path = Environment.getExternalStorageDirectory().toString() + "/Download/income_tax_return.pdf"; mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + path);` and it says the file could be found. I need this part ` file:///android_asset/pdfjs/web/viewer.html?` to work with pdf.js. The file is in the directory. – TheBook Apr 19 '17 at 09:42
  • You use loadUrl wrong. And using that path wrong. I showed you how you should load the file. How could you miss it? – greenapps Apr 19 '17 at 09:44
  • Ok. So you should load a html file from assets with your path as file parameter. You could have told that at the start. And it is not a file attribute but a file query parameter. – greenapps Apr 19 '17 at 09:47
  • If you can only supply a file name and not a full path then the html file expects the pdf file in a certain directory. Probably not the Downloads directory. Look in the documentation. – greenapps Apr 19 '17 at 09:51
  • My god.. In which directory is income_tax_return.pdf when 'it works fine'? – greenapps Apr 19 '17 at 09:53
  • So it works if the directory is getExternalStorageDirectory()? The posted code actually works!? How confusing. – greenapps Apr 19 '17 at 09:56
  • It does not work as in your code I added the following `+ "Download/income_tax_return.pdf";` – TheBook Apr 19 '17 at 09:59
  • This is my code: String path = Environment.getExternalStorageDirectory().toString() + "Download/income_tax_return.pdf"; mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + path); Please change your code to get the corrent idea. – TheBook Apr 19 '17 at 10:01
  • You should try "?file=Download/income_tax_return.pdf" when that file is in that directory. – greenapps Apr 19 '17 at 10:07
  • It does not even work with this line mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=Download/income_tax_return.pdf"); Notice I have delete the s from Downloads. I am getting `PDF.js Version 1.8.170 (build: 3ca67550 ) Missing PDF "file:///android_asset/psdfjs/web/Download/ income_tax_return.pdf"` also it is trying to access it from the project folder. – TheBook Apr 19 '17 at 10:13
  • Well if the pdf file does not load that way then i give you little chance. – greenapps Apr 19 '17 at 10:19
  • It looks as if the pdf flle is loaded from assets too when all 'goes fine'. And not from getExternalStorageDirectory(). Please elaborate. – greenapps Apr 19 '17 at 10:22
  • When doing the following `mWebView.loadUrl( "file:///android_asset/pdfjs/web/viewer.html?file=income_tax_return.pdf");` the app rendering the pdf without problem. The pdf location here is inside the project. I just need to know how can I pass the path of the download directory of my internal storage with the file name 'income_tax_return.pdf' as a parameter in the loadUrl() method. – TheBook Apr 19 '17 at 10:53
  • `The pdf location here is inside the project.` You mean that the pdf file is in the assets directory too? Like the html file? – greenapps Apr 19 '17 at 11:02
  • Yes the PDFis inside the assets directory. – TheBook Apr 19 '17 at 11:04
  • Its unbelievable that you did not start your post telling that the webview loads a html file from assets and that the used pdf file is in assets too. Basic info! Instead you confused us with that getExternalStorageDirectory in your code. A lot of time wasted. You want your js library to read pdf files from file system instead from assets. That you should have told in the intro of your post. Time wasted. – greenapps Apr 19 '17 at 11:07
  • Sorry I thought one can understand it when seeing the String of the loadUrl(). Is there a possible solution for this problem? – TheBook Apr 19 '17 at 11:12
  • Further you should have posted that error message right a way. – greenapps Apr 19 '17 at 11:27
0

What worked for me:

  try {
        String downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        String path = downloadDirectory + "/income_tax_return.pdf";
        mWebView.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" + URLEncoder.encode(path, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError("UTF-8 is unknown");
    }

or

        String downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
        String path = downloadDirectory + "/income_tax_return.pdf";
        StringBuffer buffer = new StringBuffer("file:///android_asset/pdfjs/web/viewer.html?");
        buffer.append("file="+ URLEncoder.encode(path));
        mWebView.loadUrl(buffer.toString());
TheBook
  • 1,698
  • 1
  • 16
  • 32