在运行时减小位图的大小

时间:2012-12-18 12:06:47

标签: android

我使用的是大小很大的位图。在内存中加载位图时,它给了我outofmemoryexception有没有办法减少尺寸而不改变高度,宽度和质量。

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

使用此代码 减少图像的大小,减少图像的缩小

public static Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
    int width = image.getWidth();
    int height = image.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(image, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;
}

答案 2 :(得分:0)

以下代码将在运行时为您提供屏幕分辨率。

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceHeightPix = displaymetrics.heightPixels;
deviceWidthPix = displaymetrics.widthPixels;

从中你可以在下面的函数中传递那些高度和宽度或分辨率。此函数将返回从原始样本创建的样本大小。 它不会降低任何质量。 例如,您的屏幕尺寸为480 * 800(正常和中等密度) 并且您的图片尺寸为1500 * 2000,因此首先获取您的屏幕尺寸, 传递给下面的功能,它将返回480 * 800分辨率图像而不降低其质量。

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

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    Log.d("Home", "Image Resizer -> ReqHieght : " + reqHeight);
    Log.d("Home", "Image Resizer -> ReqWidth  : " + reqWidth);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);
    Log.d("Home", "Image Resizer -> Samplesize:" + options.inSampleSize);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

要使用上述功能,

Bitmap bitmap = DecodeSampledBitmapFromResource(getResources(),R.drawable.home_screen,480,800);

使用此功能的好处是,您无需创建TWO的{​​{1}}个对象。 第一个对象包含Bitmap图像,第二个对象使用第一个对象decoded

答案 3 :(得分:0)

/**
 * decodes image and scales it to reduce memory consumption
 * 
 * @param file
 * @param requiredSize
 * @return
 */
public static Bitmap decodeFile(File file, int requiredSize) {
    try {

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, o);

        // The new size we want to scale to

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < requiredSize
                    || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;

        Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file),
                null, o2);

        return bmp;

    } catch (FileNotFoundException e) {
    } finally {
    }
    return null;
}