位图德codeStream内存不足异常 [英] Bitmap decodeStream OutOfMemory Exception
问题描述
我用我自己的实施 ViewFlow
例如对于Android在我的应用程序。我下载的加密图像来自Web服务,比拯救他们抛弃在SD卡。我使用viewflow解密图像的飞行,并告诉他们。但问题是,当用户开始更改图像太快它扔我一个 OutOfMemoryException异常
和所有我发现的信息/测试没有我的情况的工作。下面是我使用的是什么:
I'm using my own implementation of ViewFlow
example for Android in my application. I'm downloading encrypted images from web service and than save 'em on SD Card. I'm using viewflow to decrypt images on the fly and show them. But the problem is that when user start changing the images too fast it's throwing me an OutOfMemoryException
and all the information that I've found/test doesn't work for my situation. Here is what I'm using :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image_item, null);
}
try {
File bufferFile = new File(ids.get(position));
FileInputStream fis = new FileInputStream(bufferFile);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
CipherInputStream cis = new CipherInputStream(fis, cipher);
BitmapFactory.Options o = new BitmapFactory.Options();
final int REQUIRED_SIZE=300*1024;
//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<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap ops = BitmapFactory.decodeStream(cis,null,o2);
((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);
cis.close();
fis.close();
System.gc();
} catch (Exception e) {
e.printStackTrace();
((ImageView) convertView.findViewById(R.id.imgView)).setImageResource(R.drawable.image_unavailablee);
}
return convertView;
}
和它仍然在网上抛出我,异常:
And it's still throwing me that exception on line :
((ImageView的)convertView.findViewById(R.id.imgView))setImageBitmap(OPS);
与此异常:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6791KB, Allocated=3861KB, Bitmap Size=26006KB)
任何想法如何解决这个问题?
Any ideas how to fix that?
推荐答案
仅供参考,以人谁是处理大量位图存在,显示如何处理这类问题,避免出现OutofMemory的最好方法的文章!
Just for reference to anyone who is dealing with large bitmaps there is an article that show how is the best way to deal with this kind of problems to avoid OutofMemory!
<一个href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html">http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
希望它能帮助!
这篇关于位图德codeStream内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!