我用谷歌开源项目 libwebp 在安卓 1.6--android 4.0 上显示 webp 图片,直接在安卓 4.0 或更高版本上解码 webp,我发现在一些安卓手机如 appo 中,运行 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中进行垃圾回收。这可能会释放足够的内存。如果不断出现内存不足错误,请尝试查找代码中的内存泄漏并修复它。
仍然不工作?也许在本机代码的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;
}
在应用程序标记内的清单.xml文件中添加属性android:largeHeap="true"。如果您在 android 中使用大量位图,请使用缓存概念。如果您不再使用它,也不要忘记回收位图。基本上System.gc()方法不适用于位图。