位图decodeStream OutOfMemory Exception

And*_*oid 5 android bitmap out-of-memory

ViewFlow在我的应用程序中使用自己的Android实例.我正在从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;
}
Run Code Online (Sandbox Code Playgroud)

它仍然在线上抛出我的异常:

((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);

有这个例外:

java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6791KB, Allocated=3861KB, Bitmap Size=26006KB)
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决?

Leo*_*ena 7

仅供参考处理大位图的人参考,有一篇文章说明如何处理这类问题以避免OutofMemory的最佳方法!

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

希望能帮助到你!


Try*_*ain 2

摘自这里: http: //www.memofy.com/memofy/show/1008ab7f2836ab7f01071c2dbfe138/outofmemory-exception-when-decoding-with-bitmapfactory

尝试这个:

BitmapFactory.Options o = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path, options);
Run Code Online (Sandbox Code Playgroud)

代替:

BitmapFactory.Options o = new BitmapFactory.Options();
    final int REQUIRED_SIZE=300*1024;
Run Code Online (Sandbox Code Playgroud)

因此,在使用 BitmapFactory.decodeFile() 之前创建一个 16kb 的字节数组,并将其传递到解码过程中的临时存储。

希望有帮助!引用:将图像加载到位图对象时出现奇怪的内存不足问题