在设备上创建位图时内存不足 [英] Out of memory while creating bitmaps on device

查看:18
本文介绍了在设备上创建位图时内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理高分辨率图像时遇到问题.

im having problems with high resolution images.

我对 1280x720 图像使用 nodpi-drawable 文件夹,并使用此代码对其进行缩放.

Im using nodpi-drawable folder for 1280x720 images, and using this code to scale it.

public static Drawable scaleDrawable(Drawable d, int width, Activity cxt)
    {
        BitmapDrawable bd = (BitmapDrawable)d;

        double oldWidth = bd.getBitmap().getWidth();
        double scaleFactor = width / oldWidth;

        int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor);
        int newWidth = (int) (oldWidth * scaleFactor);

        Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWidth));

        BitmapDrawable bd2 = (BitmapDrawable)drawable;

        return  drawable;
    }

    public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

        int width = bm.getWidth(); 
        int height = bm.getHeight(); 

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // create a matrix for the manipulation
        Matrix matrix = new Matrix();

        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

        return resizedBitmap; 
        }

我使用该代码将图像缩放到屏幕,因此如果屏幕是 320x480,图像将缩放到 320 并保持比例,我不在乎图像是否从底部超出屏幕.

I use that code to scale images to screen witdh so if the screen is 320x480 the image will scale to 320 and keep proportions, i dont care if image go out of screen from bottom.

一切正常,但在 xhdpi 设备上尝试时,特别是在屏幕为 720x1280 的三星 Galaxy Note 2 上.

All its working fine, but when trying in a xhdpi device specifically a Samsung Galaxy Note 2 with a screen of exactly 720x1280.

它在行中因内存不足异常而崩溃:

It crash with Out Of Memory Exception in the line:

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

我不明白为什么,图像应该从 720 缩放到 720,但我的代码一定是非常糟糕的优化之类的.

I cant understand why, the image should be scaled from 720 to 720 but my code must be really bad optimized or something.

我还没有尝试过 1080x1920 的设备,但它似乎也会崩溃.

I havent tried on a 1080x1920 device but it seems it will crash too.

有人在查看代码时会看到一些不好的东西吗?

Someone can see something bad when looking at the code?

推荐答案

使用此方法调整位图大小-

use this method to resize your bitmap-

 Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);

使用这个定义-

 public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
    int reqHeight) {

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

options.inSampleSize = calculateInSampleSize(options, reqWidth,
        reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}
}
  public 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) {
    if (width > height) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    } else {
        inSampleSize = Math.round((float) width / (float) reqWidth);
     }
 }
 return inSampleSize;
}

如果您正在使用资源,则将方法替换为 BitmapFactory 的 decodeResource 方法..

If you are using resource then replace method with BitmapFactory's decodeResource method..

 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

....
.....
return BitmapFactory.decodeResource(res, resId, options);
}

这篇关于在设备上创建位图时内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆