2

I am trying to download HTML files from server then load it in webview. The files are being downloaded but they are not loading in the webview. Webview says net::ERR_FILE_NOT_FOUND even after restarting the app when the download completes.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView);
    button = (Button) findViewById(R.id.button);
    webView.loadUrl("file://"+Environment.DIRECTORY_DOWNLOADS+File.separator+"test.html");
    Log.e("CHECKKK", String.valueOf(Environment.DIRECTORY_DOWNLOADS+File.separator+"test.html"));

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                myDownload("http://xxxxxx.com/xxxx/test.html");
                myDownload("http://xxxxxx.com/xxxx/mypic1.jpg");
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, e.getMessage() + "\n" + e.getCause(), Toast.LENGTH_LONG).show();
            }
        }
    });
}

public void myDownload(String myURL) {

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myURL));
    request.setTitle("File Download");
    request.setDescription("Downloading....");
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    String nameOfFile = URLUtil.guessFileName(myURL, null, MimeTypeMap.getFileExtensionFromUrl(myURL));
    request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS, nameOfFile);

    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}
akkk
  • 1,457
  • 4
  • 23
  • 41

2 Answers2

1

Calling webView.loadUrl() should work but I think you are missing a slash in your filepath string. You could try "file:///"+Environment.DIRECTORY_DOWNLOADS+File.separator+"test.html"

Notice the extra slash in file:///

You may also not be accessing the downloads folder correctly. Try doing it this way instead:

File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File htmlFile = new File(file.getAbsolutePath()+"/test.html");
webView.loadUrl(htmlFile.getAbsolutePath());

UPDATE

It seems a mix of some of these methods was required. The correct format was: webView.loadUrl("file://"+htmlFile.getAbsolutePath());

NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29
  • Just a guess since most other examples I've seen do it that way. Another issue may be the way the downloads folder is being accessed. I've added some code above which shows another way of doing that which may solve the issue. – NoChinDeluxe Jul 26 '16 at 22:12
  • Yes. This looks better. This is the normal way to determine the download directory. But now you omitted file://. `webView.loadUrl("file://"+htmlFile.getAbsolutePath());`. – greenapps Jul 27 '16 at 08:58
  • @greenapps `webView.loadUrl("file://"+htmlFile.getAbsolutePath());` worked! :) Thanks – akkk Jul 28 '16 at 16:15
  • @akkk - I added your final solution to my answer above. Feel free to add your own answer or accept this one. Up to you. – NoChinDeluxe Jul 28 '16 at 16:18
  • @greenapps Thanks for your input. You are more than welcome to create your own answer to have it accepted. But I do feel my answer was helpful. – NoChinDeluxe Jul 28 '16 at 16:21
  • `Notice the extra slash in file:///`. Well that was not very helpfull i think. And you should not have made that `UPDATE` of course as it was not your idea. – greenapps Jul 28 '16 at 16:27
0

Read the file into a String and then use

// read from file
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134