我使用google开源项目libwebp在android 1.6--android 4.0上显示webp图像,在android 4.0或以上版本直接解码webp,发现在appo等一些android手机上运行时会报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;
}