我在我的应用程序中使用自己实现的Android ViewFlow
示例。我从Web服务下载加密图像,然后将它们保存到SD卡上。我使用viewflow动态解密图像并显示它们。但是问题是,当用户开始快速更改图像时,它会抛出OutOfMemoryException
异常,而我找到/测试的所有信息都无法解决我的情况。这是我正在使用的:
@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;
}
我还是在这一行代码抛出了异常:
((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);
异常信息如下:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6791KB, Allocated=3861KB, Bitmap Size=26006KB)
有什么想法来解决这个问题吗?