【发布时间】:2013-11-26 01:57:32
【问题描述】:
我正在使用 webview 来显示图像。我使用了 webview.loadUrl("imagepath");这些代码显示图像但 webview 无法加载图像然后我想在 webview 中显示我的默认图像。
【问题讨论】:
标签: android webview android-webview
我正在使用 webview 来显示图像。我使用了 webview.loadUrl("imagepath");这些代码显示图像但 webview 无法加载图像然后我想在 webview 中显示我的默认图像。
【问题讨论】:
标签: android webview android-webview
您必须在图像路径前添加file:///。
你尝试过类似的东西
// Exemple from asset folder
webview.loadUrl("file:///android_asset/mu_image.jpg");
// From external storage
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/YOUR_FILE.jpg";
webview.loadUrl(base);
编辑(理解问题后...)
你必须检查你的网络文件是否存在
public static boolean exists(String URLName){
try {
HttpURLConnection.setFollowRedirects(false);
// note : you may also need
// HttpURLConnection.setInstanceFollowRedirects(false)
HttpURLConnection con =
(HttpURLConnection) new URL(URLName).openConnection();
con.setRequestMethod("HEAD");
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
然后有 2 个选择: - 文件存在,显示它 - 或者,显示您的默认图像
【讨论】:
【讨论】: