- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在特定时间缓存 WebView 中显示的图像,例如7 天,所以我需要将图像缓存保存到磁盘并从磁盘加载它们并将它们提供给 WebView。
我还需要调整图像的大小以避免 WebView;内存使用率高时崩溃,因此我在名为“fetchBitmap”的阻塞函数中实现了缓存和调整大小逻辑。
正如 Android 文档所述,“shouldInterceptRequest”在 UI 线程以外的线程上运行,因此我可以在此函数中进行联网,但正如我所见,阻塞调用会导致要卡住的 WebView。
该函数的行为方式迫使我使用阻塞 调用,并且我不能为例如以备将来完成。
有什么解决方法吗?
webView.setWebViewClient(new WebViewClient() {
private boolean isImage(String url) {
if (url.contains(".jpg") || url.contains(".jpeg")
|| url.contains(".png")) {
return true;
}
return false;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (isImage(url)) {
Bitmap bitmap = ImageUtil.fetchBitmap(url);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
return new WebResourceResponse("image/*", "base64", bs);
}
return null;
}
});
--更新--
OK,我改变了逻辑,现在我只是从磁盘 block 读取,并在一个runnable中单独处理网络负载(缓存); "fetchBitmapForWebView";当缓存不可用时,它会使网络负载加倍,但 UI 现在的响应速度更快。
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (isImage(url)) {
Bitmap bitmap = ImageUtil.fetchBitmapForWebView(url);
if (bitmap == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
return new WebResourceResponse("image/*", "base64", bs);
}
return null;
}
最佳答案
一般原则是你不应该在 shouldInterceptRequest
中做任何繁重的处理,因为它是一个同步调用(尽管是在另一个线程上)。所有网络请求的调用都在同一个线程上处理,因此阻塞一个请求将导致所有其他请求也被阻塞。
对于您的用例,我会考虑在您的应用程序中运行一个小型网络服务器,它将执行所有必需的缓存和处理。当然,您还需要重写显示的网页中的所有链接,以便指向本地服务器而不是原始 Web 服务器,因此在某些情况下可能会很麻烦。但作为一种通用方法,这将极大地有助于卸载繁重的工作并维护本地缓存。
周围有许多简单的 Java Web 服务器,例如:
关于android - WebViewClient shouldInterceptRequest 卡住 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30180529/
作为一名开发人员,在构建在 3G 连接的 Android webviewclient 上运行的应用程序(带有网页)时,我有过非常糟糕的经历。我的一些用户说有些页面需要 10 分钟才能加载。 (我不知道
我正在使用以下方法将 URL 加载到我的 WebView 中: mWebView.setWebviewClient(new WebViewClient); 此 URL 加载 Http 身份验证页面。用
这是我上周提出的一个问题的重新发布,得到了 2 个无效的答案。 我遵循了关于 http://developer.android.com/resources/tutorials/views/hello-
当我调用 WebView#loadUrl我希望我应该只得到一个 WebViewClient#onPageFinished打电话但没有WebViewClient#shouldOverrideUrlLoa
我是 Android 应用开发的新手。我正在设法显示 WebView 并加载给定的 URL。当我单击 WebView 中的链接时,出现空白屏幕。 当我在设备 (Galaxy TAB) 上使用 Chro
我的应用程序中有一个 webView。当一个人加载一个 url 时,该网页将加载到浏览器中,而不是我的应用程序中,因为选项菜单是默认的,而不是我分配的。我怎样才能阻止它并让它加载到我的 WebView
WebView有一个概述,这有助于理解目的、预期流程以及如何使用它,但是 WebViewClient ,出于某种原因,没有概述。 这个类的目的是什么?为什么它包含的方法有一个单独的类?其生命周期中典型
我在 S/O 上看过几篇类似的帖子,但找不到我的问题的答案。我正在尝试从 Web View 中获取 html,但无法触发“onPageFinished() 回调。我什至尝试在 WebChromeCli
下面是我的 WebViewClient 代码。我通过单击按钮调用了该类。 public void onCreate(Bundle savedInstanceState) { super
WebViewClient.onReceivedError 已弃用,现在我必须使用 onReceivedHttpError 来处理 webview 错误,但是此方法从任何资源接收错误,这不是我想要的欲
我无法理解的简短问题,我想在人们单击超链接后从我的 WebView 启动一个新的浏览器。但是我如何设置该链接的目标以逃避 WebViewClient? 这是我的代码,非常感谢任何帮助: WebView
我想问一下目的 WebViewClient.onLoadResource 当 webkit 尝试加载资源时,我们甚至可以得到这个回调但是因为它的返回类型是 void 我们可以在这个函数中做些什么???
我想创建一个 Intranet 应用程序。这个应用程序将显示内容,通常只能在我们的内部环境中访问。例如http://intranet.ourfirm.com 现在我们可以从外部访问此内容例如https
我无法理解的简短问题,我想在人们单击超链接后从我的 WebView 启动一个新的浏览器。但是我如何设置该链接的目标以逃避 WebViewClient? 这是我的代码,非常感谢任何帮助: WebView
在我的 WebView 中,标题中有多个链接。在离线模式下,我不允许重新加载页面并保持该页面不变,然后通知用户有关互联网连接的信息。如果我单击其他链接,我可以使用 shouldOverrideUrlL
问题很简单。在应用程序中,我们希望跟踪显示的当前 url。为此,我们使用来自 WebViewClient 的 shouldOverrideUrlLoading 回调,方法是在每次更新时将 url 保存
好的,所以我得到了这些错误我不知道为什么我得到这个,但它似乎对世界其他地方适用,如果有另一种方法我必须去做这件事我会改变代码但现在看来我的 eclipse 不喜欢我非常喜欢 The method on
我想捕捉本地 anchor 点击。这些 anchor 在页面内是本地的,即 href="#link_1"。捕捉这些点击的最佳方式是什么? OverrideURL 不会触发它们?我正在加载一个基于 an
我想拦截 WebViewClient 中的所有 HTTP 请求并发出我自己的请求。我遇到的问题是处理压缩响应。 有几个不同的帖子建议可行的方法(例如 AndroidHttpClient.getUngz
我在这里看到了几篇关于实现 WebviewClient 的文章,以便 webview 中的转换保留在 webview 中而不是转到浏览器。 当我尝试运行我的应用程序时,webview 加载但它仍然没有
我是一名优秀的程序员,十分优秀!