与TouchImageView一起使用bitmapfactory时的OutOfMemoryError

时间:2012-08-31 10:57:51

标签: android android-imageview out-of-memory android-image bitmapfactory

我正在尝试制作一个片段,使用TouchImageView显示可缩放的图像(https://github.com/MikeOrtiz/TouchImageView)

该片段还有一个用于更改图像的微调器。 问题是第一个图像加载正常,但是当我使用滚动条更改图像时,我得到一个OutOfMemoryError并且程序崩溃。这是我的代码

public class mapFragment extends SherlockFragment {


String[] Levels = { "Ground Floor", "First Floor",
        "Second Floor", "Third Floor"
};

Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
{
    View v = inflater.inflate(R.layout.maps_layout, group, false);


    final TouchImageView img = (TouchImageView) v.findViewById(R.id.touchimage1);
    final Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.groundfloor);
    img.setImageBitmap(snoop);




    final Spinner s = (Spinner) v.findViewById(
            R.id.spinnerlevels);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this
            .getActivity().getBaseContext(),
            android.R.layout.simple_spinner_item, Levels);
    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, 
                int pos, long id) {
            // An item was selected. You can retrieve the selected item using
            // parent.getItemAtPosition(pos)
            int item = s.getSelectedItemPosition();



            if(item ==0){
                snoop.recycle();
                Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.groundfloor);
                img.setImageBitmap(snoop);
            }
            if(item ==1){
                snoop.recycle();
                Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.firstfloor);
                img.setImageBitmap(snoop);
            }
            if(item ==2){
                snoop.recycle();
                Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.secondfloor);
                img.setImageBitmap(snoop);
            }
            if(item ==3){
                snoop.recycle();
                Bitmap snoop = BitmapFactory.decodeResource(getResources(), R.drawable.thirdfloor);
                img.setImageBitmap(snoop);
            }


        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }
    });



    img.setMaxZoom(8f);

    return (v);


}

}

不应该“recylce()”删除第一张图片,以便为内存中的新图像添加位置? MB的图像大小为1.4,1.5,1.5,1.3

2 个答案:

答案 0 :(得分:1)

  

不应该“recylce()”删除第一张图片,以便为新图片添加位置   一个在记忆中?

不,recycle()方法只是将此位图标记为“死”,并且可以在稍后的某个时刻进行垃圾收集。以下是recycle()方法的文档:

  

释放与此位图关联的本机对象,然后清除   参考像素数据。这不会释放像素数据   同步;它只是允许它被垃圾收集,如果有的话   没有其他参考。位图标记为“死”,意思是它   如果调用getPixels()或setPixels(),则会抛出异常   什么都不会。这个操作无法逆转,所以应该如此   只有在你确定没有进一步的用途时才被调用   位图。这是一个高级调用,通常不需要调用,   因为正常的GC过程会在没有时释放这个内存   更多对此位图的引用。

答案 1 :(得分:0)

对于图像的每个像素,您的堆使用8个字节。现在有多少内存是1754 x 2481 x 8?答案是32.94 MB的堆内存。在许多设备上,你不会有超过16 MB的堆,这也用于其他东西。你现在得到了问题吗?

您需要缩小图片或者您的应用永远无法飞行:)