大图像抛出 OutOfMemoryError

Throwing OutOfMemoryError with large images

我正在研究如何在我的 "universal" 应用程序中保持图像(图标和图像)的相同方面。

resolutions/screens 密度的问题让我很头疼。

有什么方法可以解决这个问题:?

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

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

我的解决方案是处理大图像并将其缩小到不显示像素化的图像。

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

这是我的一部分 activity

    @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;
        }

单击图像时,我更改了 image.When 我更改了几个图像,内存不足无法分配...显示异常并关闭应用程序。

有什么想法吗?

非常感谢

以下内容在类似情况下对我有用: 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;

    }

});