gpt4 book ai didi

java - 将大图片 (5 MB) 或 GIF 加载到 WebView 时内存不足

转载 作者:行者123 更新时间:2023-11-30 11:18:09 25 4
gpt4 key购买 nike

我不想使用 WebView.loadUrl(String url) 因为我希望能够在没有 Internet 连接时加载图像。

现在,我有这个下载辅助函数:

public static byte[] downloadBinaryContent(String url) {
HttpURLConnection hcon = getConnection(url);
if (hcon == null)
return null;
try {
InputStream is = hcon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

ByteArrayBuffer baf = new ByteArrayBuffer(8192);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
CacheHandler.writeBinary(url, baf.toByteArray()); // keep it for offline use
return baf.toByteArray();
} catch (IOException e) {
Log.d("Download Binary Failed", e.toString());
return null;
}
}

这是为了显示(我可以使用 ImageView 但我也想轻松显示 GIF):

        final WebView web = (WebView) opView.findViewById(R.id.my_webview);
new Thread() {
public void run() {
String url_wiki = "http://upload.wikimedia.org/wikipedia/commons/2/2d/Snake_River_%285mb%29.jpg";
byte[] binary_content = getBinaryContent(url_wiki);
final String b64Image = Base64.encodeToString(binary_content, Base64.DEFAULT);
handler.post(new Runnable() {
@Override
public void run() {
web.loadData(b64Image, "image/jpeg", "base64");
}
});
}
}.start();

这里调用的函数 getBinaryContent() 基本上检查缓存文件,如果没有,它会调用 downloadBinaryContent() 来下载它。

如果我将 URL 替换为较小的图片,这些工作正常,但当它太大时失败。我怀疑问题的原因是我正在加载所有 byte[] 并将它们编码为内存中的 String 。但是我怎样才能避免这种情况呢?

最佳答案

尝试将此添加到您的 list 中,在 <Application> 中标签。

 android:largeHeap="true"

这将为您的应用请求更大的堆空间。

文档位于 here .

您已指定“失败”,但未能提供失败的任何支持证据。上述解决方案假设您在处理大型位图时遇到 OutOfMemmory 异常,这通常是这种情况。如果上述方法未能解决您的问题,请提供更详细的“失败”解释,并提供 logcat 日志(如果适用)。

关于java - 将大图片 (5 MB) 或 GIF 加载到 WebView 时内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23971822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com