gpt4 book ai didi

android - 当我使用 libwebp 和 ndk 在 android 2.x 上显示 webp 图像时出现内存不足错误

转载 作者:行者123 更新时间:2023-11-30 03:05:37 26 4
gpt4 key购买 nike

‖我在android 1.6--android 4.0上使用google开源项目libwebp显示webp图片,在android 4.0以上版本直接解码webp,我发现在一些android手机如appo上会出现outofmemory运行libwebp lib时出错,报错代码为:int[] pixels = new int[decoded.length/4];谁能建议我如何避免这种情况?这是我的代码:

‍‍

public static Bitmap getBitmapFromURL(String src) {        
HttpURLConnection connection = null;
Bitmap bmp = null;
try {
connection = (HttpURLConnection)
new URL(src).openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"image/webp");
connection.connect();
//Send request
DataOutputStream wr = new DataOutputStream ( connection.getOutputStream ());
wr.writeBytes ("");
wr.flush ();
wr.close ();
//Get Response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
while(true) {
String n = rd.readLine();
if(n == null)break;
baos.write(n.getBytes(), 0, n.getByte().length);
baos.write('\n');
}
byte data[] = baos.toByteArray();
// From the lib's exemple
int[] width = new int[] { 0 };
int[] height = new int[] { 0 };
int test = libwebp.WebPGetInfo(data, data.length, width, height);
// test = 0 ! ! !
byte[] decoded = libwebp.WebPDecodeARGB(data, data.length, width, height);

#######################out of memory error is always here
int[] pixels = new int[decoded.length / 4];
#######################
ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
bmp = Bitmap.createBitmap(pixels, width[0], height[0],Bitmap.Config.ARGB_8888);
} catch (IOException e) {
e.printStackTrace(); }
return bmp;
}

最佳答案

在创建位图之前尝试使用 System.gc()。这将强制在 Android 中进行垃圾收集。这可能会释放足够的内存。如果您不断遇到内存不足错误,请尝试在您的代码中查找内存泄漏并修复它。

还是不行?也许在 native 代码的 C/C++ 中尝试它。在这里您可以有效地管理您的内存(如果您熟悉 C/C++)。

尝试这样的事情:

public static ByteArrayOutputStream getBytesFromURL(String src) {        
HttpURLConnection connection = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
connection = (HttpURLConnection)
new URL(src).openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"image/webp");
connection.connect();
//Send request
DataOutputStream wr = new DataOutputStream ( connection.getOutputStream ());
wr.writeBytes ("");
wr.flush ();
wr.close ();
//Get Response
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while(true) {
String n = rd.readLine();
if(n == null)break;
baos.write(n.getBytes(), 0, n.getByte().length);
baos.write('\n');
}
connection.close();
}catch(Exception q){} //Your exception catching here
return baos;
}

public static Bitmap createBitmap(ByteArrayOutputStream baos){
System.gc();
byte data[] = baos.toByteArray();


// From the lib's exemple
int[] width = new int[] { 0 };
int[] height = new int[] { 0 };
int test = libwebp.WebPGetInfo(data, data.length, width, height);
// test = 0 ! ! !
byte[] decoded = libwebp.WebPDecodeARGB(data, data.length, width, height);
int[] pixels = new int[decoded.length / 4];
ByteBuffer.wrap(decoded).asIntBuffer().get(pixels);
bmp = Bitmap.createBitmap(pixels, width[0], height[0],Bitmap.Config.ARGB_8888);
return bmp;
}

关于android - 当我使用 libwebp 和 ndk 在 android 2.x 上显示 webp 图像时出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21875784/

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