在设备上的外部存储目录中显示WebView中的图像

时间:2013-08-05 13:46:11

标签: android android-webview base-url android-external-storage

我想在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是存储在外部存储目录中的文件。

1 个答案:

答案 0 :(得分:0)

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

code to access SDCARD in nexus 7