位图解码的3001616字节分配错误导致内存不足

时间:2012-04-19 08:27:36

标签: java android

我实际上是在方法中得到这个错误在里面加载图像我正在做 我将选项大小设置为:

bmOptions.inSampleSize = 1;

错误指向行decodeStream() ..

的方法
private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in, null, options);
            in.close();

        } catch (IOException e1) {

            return null;
        }
        return bitmap;

    }
E/dalvikvm-heap(8627): Out of memory on a 3001616-byte allocation.
at decodeStream()
at loadImage()

3 个答案:

答案 0 :(得分:1)

以下是将文件解码为指定比例的代码。

    File f = new File(StringfileName);
    Bitmap bm = decodeFile(f);

    private static Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_SIZE = 150;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        o.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }

    return null;
}

答案 1 :(得分:1)

这个类以高效的方式将图像缩放到精确的大小,它也会进行一些自动旋转,你可能想要禁用它或传递角度。基于帖子:http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/

import java.io.IOException;

import java.io.InputStream;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;

/**
 * http://zerocredibility.wordpress.com/2011/01/27/android-bitmap-scaling/
 * 
 */
public final class 

BitmapScaler {

    /**
     * 
     * @param uri
     * @param context
     * @param newWidth
     *            Image will not exceed this width
     * @param newHeight
     *            Image will not exceed this height
     * @return
     * @throws IOException
     */
    public static Bitmap ScaleBitmap(Uri uri, Context context,
            int newWidth, int newHeight, int targetWidth, int targetHeight)
            throws IOException {
        final ContentResolver contentResolver = context.getContentResolver();
        int sample = 1;
        {
            InputStream is = contentResolver.openInputStream(uri);
            try {
                sample = getRoughSize(is, newWidth, newHeight);
            } finally {
                is.close();
            }
        }
        {
            InputStream is = contentResolver.openInputStream(uri);
            try {
                Bitmap temp = roughScaleImage(is, sample);
                try {
                    return scaleImage(temp, newWidth, newHeight, targetWidth,
                            targetHeight);
                } finally {
                    temp.recycle();
                }
            } finally {
                is.close();
            }
        }
    }

    private static Bitmap scaleImage(final Bitmap source, final int maxWidth,
            final int maxHeight, final int targetWidth, final int targetHeight) {

        int newWidth = maxWidth;
        int newHeight = maxHeight;

        final int sourceHeight = source.getHeight();
        final int sourceWidth = source.getWidth();
        final int angle = sourceHeight > sourceWidth ? -90 : 0;
        final boolean rotate = angle != 0;
        final boolean nintey = (angle == 90) || (angle == -90);
        final int width = nintey ? sourceHeight : sourceWidth;
        final int height = nintey ? sourceWidth : sourceHeight;

        final float scaleByWidth = ((float) newWidth / width);
        int testNewHeight = (int) (height * scaleByWidth);

        float scale;

        if (testNewHeight > newHeight) { // then we must scale to match
                                            // newHeight instead of new width
            final float scaleByHeight = ((float) newHeight / height);
            newWidth = (int) (width * scaleByHeight);
            scale = scaleByHeight;
        } else {
            // accept the scale
            newHeight = testNewHeight;
            scale = scaleByWidth;
        }

        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        if (rotate) {
            matrix.postRotate(angle);
            matrix.postTranslate(0, newHeight);
        }
        matrix.postTranslate((maxWidth - newWidth) / 2,
                (maxHeight - newHeight) / 2);

        Bitmap b = Bitmap.createBitmap(targetWidth, targetHeight,
                Bitmap.Config.ARGB_8888);
        Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);
        Canvas c = new Canvas(b);
        c.drawBitmap(source, matrix, p);
        return b;
    }

    private static Bitmap roughScaleImage(InputStream is, int sample) {
        BitmapFactory.Options scaledOpts = new BitmapFactory.Options();
        scaledOpts.inSampleSize = sample;
        return BitmapFactory.decodeStream(is, null, scaledOpts);
    }

    private static int getRoughSize(InputStream is, int newWidth, int newHeight) {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);

        return getRoughSize(o.outWidth, o.outHeight, newWidth, newHeight);
    }

    private static int getRoughSize(int width, int height, int newWidth,
            int newHeight) {
        int sample = 1;

        while (true) {
            if (width / 2 < newWidth || height / 2 < newHeight) {
                break;
            }
            width /= 2;
            height /= 2;
            sample *= 2;
        }
        return sample;
    }
}

答案 2 :(得分:0)

看一下这篇文档http://developer.android.com/training/displaying-bitmaps/load-bitmap.html。我之前也遇到过同样的问题。