WebView无法从内部存储加载文件(WebView not loading the file from internal storage)

编程入门 行业动态 更新时间:2024-10-25 12:25:48
WebView无法从内部存储加载文件(WebView not loading the file from internal storage)

我试图从服务器下载HTML文件,然后在webview中加载它。 正在下载文件,但它们未加载到webview中。 即使在下载完成后重新启动应用程序,Webview也会说net::ERR_FILE_NOT_FOUND 。

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

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"

注意file:///的额外斜杠

您可能无法正确访问下载文件夹。 尝试这样做:

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

UPDATE

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

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

更多推荐

本文发布于:2023-08-06 00:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   文件   WebView   loading   storage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!