WebView无法从内部存储加载文件 [英] WebView not loading the file from internal storage

查看:175
本文介绍了WebView无法从内部存储加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务器下载HTML文件,然后将其加载到Webview中.正在下载文件,但未将其加载到Webview中.即使下载完成后重新启动应用程序,Webview也会显示net::ERR_FILE_NOT_FOUND.

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);
}

推荐答案

调用webView.loadUrl()应该可以,但是我认为您在文件路径字符串中缺少斜杠.您可以尝试"file:///"+Environment.DIRECTORY_DOWNLOADS+File.separator+"test.html"

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"

注意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());

更新

似乎需要混合使用其中一些方法.正确的格式为: webView.loadUrl("file://"+htmlFile.getAbsolutePath());

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

这篇关于WebView无法从内部存储加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆