在设备上的外部存储目录中显示WebView中的图像(Showing image in WebView from External storage Directory on device)

编程入门 行业动态 更新时间:2024-10-11 15:18:02
在设备上的外部存储目录中显示WebView中的图像(Showing image in WebView from External storage Directory on device)

我想在webview中显示一个图像,该图像已从外部下载到ExternalStorageDirectory 。 我使用followng代码将HTML作为字符串加载,基本URL设置为保存图像的位置:

Article article = page.articles.get(0); String mime = "text/html"; String encoding = "utf-8"; String base = "file:/" + Environment.getExternalStorageDirectory() + "/images_folder/"; Log.v("IMAGE", base); webView = (WebView)rootView.findViewById(R.id.webView); webView.setBackgroundColor(Color.TRANSPARENT); webView.loadDataWithBaseURL(base, article.articleContent, mime, encoding, null);

此代码在模拟器中工作正常,但是当我在设备(Nexus 7 Tablet)上尝试时,图像无法加载。 图像肯定存储在外部存储目录中,因为可以使用平板电脑上的文件浏览器找到它们。 我想这与使用模拟存储的Nexus 7有关但我真的不知道,每次调用Environment.getExternalStorageDirectory()应该指向同一个目录。

引入的html字符串非常简单,如下所示:

<html><head></head><body><img src = 'test_download1.png'></img>This is the first Article for the first page in html</body></html>

其中test_download1.png是存储在外部存储目录中的文件。

I want to show an image in a webview which has been downloaded externally to ExternalStorageDirectory. I am using the followng code to load the HTML as a string with the base URL set to be that of where the images are saved:

Article article = page.articles.get(0); String mime = "text/html"; String encoding = "utf-8"; String base = "file:/" + Environment.getExternalStorageDirectory() + "/images_folder/"; Log.v("IMAGE", base); webView = (WebView)rootView.findViewById(R.id.webView); webView.setBackgroundColor(Color.TRANSPARENT); webView.loadDataWithBaseURL(base, article.articleContent, mime, encoding, null);

This code works fine in the emulator but when I try it on a device (Nexus 7 Tablet) the image doesn't load. The image is definitely stored in the External Storage Directory as they can be found using a file explorer on the tablet. I imagine this has something to do with the Nexus 7 using emulated storage but I really don't know, the call Environment.getExternalStorageDirectory() should point to the same directory every time.

The html String being brought in is very simple and looks like this:

<html><head></head><body><img src = 'test_download1.png'></img>This is the first Article for the first page in html</body></html>

Where test_download1.png is a file stored in the External Storage directory.

最满意答案

要在nexus 7中访问SDCARD,您必须编写不同的代码,因为您的代码只能访问mnt / sdcard,而不能访问保存图像的实际路径。

在nexus 7中访问SDCARD的代码

To access SDCARD in nexus 7 you have to write a different code as your code will give access only to mnt/sdcard and not to actual path,where your image is saved.

code to access SDCARD in nexus 7

更多推荐

本文发布于:2023-07-14 19:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   设备   目录中   WebView   Showing

发布评论

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

>www.elefans.com

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