使用大图像抛出OutOfMemoryError

时间:2015-01-27 09:47:05

标签: android bitmap out-of-memory screen-resolution

我正努力在我的"通用"中保持我的图像(图标和图像)的相同方面。应用

分辨率/屏幕密度的问题让我感到很头疼。

有没有办法解决这个问题:?

设备: 平板电脑10" / 1280x752 / 屏幕密度MDPI

如果我为每个密度创建一个图像,则在分辨率较高且屏幕密度较低的设备中,图像显示较小。

我的解决方案是使用大图像并将其缩小到未显示像素化的图像。

我正在使用官方解决方案Android

这是我活动的一部分

    @Override   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthScreen = size.x;
        heightScreen = size.y;      
        imgTest = (ImageView)findViewById(R.id.image);

        imgTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

           final Bitmap img = decodeSampledBitmapFromDrawable(Test.this,tipoprueba, widthScreen, heightScreen);
        imgTest.setImageBitmap(img);

                    }

        });

    }


public static Bitmap decodeSampledBitmapFromDrawable(Context activity,String res, int reqWidth, int reqHeight) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(activity.getResources(), activity.getResources().getIdentifier(res, "drawable", "com.example.docaper"), options);
        options.inSampleSize = Utils.calculateInSampleSizeInside(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inScaled = false;
        return BitmapFactory.decodeResource(activity.getResources(), activity.getResources().getIdentifier(res, "drawable", "com.example.docaper"), options);
    }



    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;

        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
        }

单击图像时,我会更改图像。当我更改多个图像时,OutOfMemory无法分配...显示异常并关闭应用程序。

有什么想法吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

以下在类似的情况下对我有用: 在它超出范围之后,Android并没有真正释放位图。留下很高的内存使用率通常不会被清除。

保留您正在设置的位图的句柄

private Bitmap storedBitmap = null;

OnClickListener()更改为:

imgTest.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

       final Bitmap img = decodeSampledBitmapFromDrawable(Test.this,tipoprueba, widthScreen, heightScreen);
       imgTest.setImageBitmap(img);

       if(storedBitmap != null){
           storedBitmap.recycle();
           storedBitmap = null;
       }
       storedBitmap = img;

    }

});