- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的函数拦截所有对 webview 的请求。就像加载页面一样。
@Override
public WebResourceResponse shouldInterceptRequest(final WebView view,
String url) {...}
所有 html、css 和 js 文件都已正确回答,但是当我想发送 png 或 gif 图像作为响应时,它不起作用。他们可能需要特殊的 MIME
类型,但我无法让它工作。
不得不说,我要发送的图像是通过 InputStream
中的 HttpURLConnection
接收的,并转换为 String
,并保存在一个文件夹;因此,当我需要图像时,我只需获取该文件(一个 String
)并将其转换为 InputStream
。
InputStream is = new ByteArrayInputStream(imageString.getBytes());
return new WebResourceResponse("text/html", "UTF-8", is);
我试过 image/gif
、image/png
都没有用。
有什么想法吗?
最佳答案
输出流需要是FileOutputStream
的
图片需要以字节格式保存,不需要编码。
请记住,您需要保留图像文件扩展名。例如,如果您正在下载 image.png
并将其保存为 image.tiff
,它将不起作用。
这是我下载图片的方式:
URLConnection conn;
BufferedInputStream bistream = null;
BufferedOutputStream bostream = null;
boolean failed = false;
try
{
conn = new URL("http://../image.png").openConnection();
bistream = new BufferedInputStream(conn.getInputStream(), 512);
byte[] b = new byte[512];
int len = -1;
bostream =
new BufferedOutputStream(
new FileOutputStream(new File("/../image-downloaded.png")));
while((len = bistream.read(b)) != -1)
{
bostream.write(b, 0, len);
}
}
catch(Exception e) // poor practice, catch each exception separately.
{ /* MalformedURLException -> IOException -> Exception */
e.printStackTrace();
failed = true;
}
finally
{
if(bostream != null)
{
try
{
bostream.flush();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
bostream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
if(bistream != null)
{
try
{
bistream.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
if(failed == false)
{
//code
}
else
{
// code
}
关于java - 回答 .gif 或 .png 图像 WebResourceResponse 到 shouldInterceptRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436667/
我想知道在为 android api 级别 21 (lollipop) 添加 setstatuscodeandReasonPhrase 方法之前使用了什么,我正在使用旧版本 (kitkat) 并计划使
我正在寻找一种创建XWalkWebResourceResponse 的方法。我的案例如下。 我正在创建将拦截所有网络请求的自定义 XWalkResourceClient 客户端。所有应该被拦截的请求都
我正在尝试在 WebView 中获取请求的 POST 响应。这是我的代码。 mWebview.setWebViewClient(new WebViewClient() { @Override
我的函数拦截所有对 webview 的请求。就像加载页面一样。 @Override public WebResourceResponse shouldInterceptRequest(final We
public WebResourceResponse shouldInterceptRequest(WebView View , WebResourceRequest 请求)没有反应。我的构建等级:
我尝试按照本教程进行操作: http://docs.phonegap.com/en/2.7.0/guide_getting-started_android_index.md.html#Getting%
我有这样的功能: public WebResourceResponse shouldInterceptRequest(WebView view, String url) { i
我是一名优秀的程序员,十分优秀!