您可以使用此方法来解码图像。它可能会使它们更轻。请记住,图像在显示增加其内存消耗时会变成位图。
public static Bitmap decode(byte[] imageByteArray, int width, int height) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length,
o);
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp/2 < width || height_tmp/2 < height)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.inTempStorage = new byte[Math.max(16 * 1024, width * height * 4)];
return BitmapFactory.decodeByteArray(imageByteArray, 0,
imageByteArray.length, o2);
// return BitmapFactory.decodeByteArray(imageByteArray, 0,
// imageByteArray.length);
}
其中
宽度 - 在该ImageView的可能像素最大宽度。 高度 - imageView可能具有的最大像素高度(以像素为单位)。
这样,位图会变得更轻,应用程序可能会消耗更少的内存。
(注:我复制此方法并修改了它一点点,我不记得原来的问题,所以我不能把URL)
我用strore图像的字节数组,我只解码他们就在展示他们之前。
现在,就像James L所说,最好将图像保存在文件系统中,并且只在需要时才会记忆,但如果你不这样做(我的情况)。你可以下载图片:
public static byte[] getBytes(InputStream is) throws IOException {
int len;
int size = 1024;
byte[] buf;
if (is instanceof ByteArrayInputStream) {
size = is.available();
buf = new byte[size];
len = is.read(buf, 0, size);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1)
bos.write(buf, 0, len);
buf = bos.toByteArray();
}
return buf;
}
public static byte[] downloadFileByteArray(String fileUrl)
throws IOException, MalformedURLException {
URL myFileUrl = null;
myFileUrl = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
return getBytes(is);
}
如果媒体链接在内存中的图像,你将能够通过我提出的方法寻找热塑成型将它们变成字节数组。
除此之外(并调用System.gc()),你可以做的事情并不多(据我所知)。也许在onPause()和onDestroy()中使用BMP并在onResume()中重新构造它们(如果需要的话)。
好吧,高质量我的意思是图像都加载并显示为ARGB_8888,其他响应者缩放它们的建议并不有用,因为所有图像的创建尺寸都与它们在屏幕上显示所需的尺寸完全相同。本质上,应用程序创建图像的小动画。想想它就像我在运行时创建southpark情节一样。加载的唯一图像是用于“场景”的图像,当切换到下一个时,我拥有当前和下一个“场景”的图像。图像本质上是“场景”中任何移动主体的“部分”。 – Hamid 2011-04-06 20:55:20
基于这里的解决方案不是加载图像的方法而被接受,因为无论如何,它们仍然必须被位图解码,但是只有当前使用的图像需要保存在内存中。 – Hamid 2011-08-01 23:54:16