我想在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
更多推荐
发布评论