【问题标题】:Throwing OutOfMemoryError with large images用大图像抛出 OutOfMemoryError
【发布时间】:2015-01-27 09:47:05
【问题描述】:

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

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

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

设备: 平板电脑 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 无法分配...显示异常并关闭应用程序。

有什么想法吗?

非常感谢

【问题讨论】:

  • 我认为问题与图像调整大小或缩放无关,它与显示许多图像有关。在将图像显示到 gridview 或任何其他视图中时,请保持较小的尺寸,而仅当用户单击要查看的图像时才以全尺寸显示图像,与画廊应用程序相同。还要使用可重复使用的视图。

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


【解决方案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;

    }

});

【讨论】:

  • 你的意思是img = null还是storedBitmap=null
  • storedBitmap=null 只是我倾向于做的事情,总是在位图为 recycle() 之后将其设置为 null
  • 我可以直接设置img = null而不是创建位图storedbitmap链接吗?
  • 关于我的问题的第一部分,如何在不使用android:layout_width="match_parent" 的情况下将图像(正方形)缩放到屏幕(横向)的总高度并保持纵横比?我需要保持纵横比,同时检测图像而不是背景何时被触摸。Thx
  • 让它检测触摸。在活动的代码中查看clickable=trueonClick=image_touched 确保你有public void image_touched(View v){ //handle click here } 至于保持它的最佳选择可能是创建ImageView 的子类并覆盖OnMeasure() Like this example跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
  • 2018-09-02
  • 2021-01-01
  • 1970-01-01
相关资源
最近更新 更多