Android中位图大小超过VM预算

Android中位图大小超过VM预算,android,android-image,Android,Android Image,我在库视图中使用了以下代码,得到了以下异常。谁能告诉我哪里错了 07-18 12:45:59.774: WARN/System.err(6758): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 07-18 12:45:59.774: WARN/System.err(6758): at android.graphics.Bitmap.nativeCreate(Native Method) 07-18 12:45:59.

我在
库视图中使用了以下代码,得到了以下异常。谁能告诉我哪里错了

07-18 12:45:59.774: WARN/System.err(6758): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
07-18 12:45:59.774: WARN/System.err(6758):     at android.graphics.Bitmap.nativeCreate(Native Method)
07-18 12:45:59.774: WARN/System.err(6758):     at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
07-18 12:45:59.774: WARN/System.err(6758):     at android.graphics.Bitmap.createBitmap(Bitmap
我的代码:

public boolean createReflectedImages() {
    //The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    int index = 0;
    for (int imageId : mImageIds) {
        try
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inTempStorage = new byte[16*1024];

            Bitmap originalImage = BitmapFactory.decodeResource(getResources(), 
                            imageId,options);
            int width = originalImage.getWidth();
            int height = originalImage.getHeight();

            //This will not scale but will flip on the Y axis
            Matrix matrix = new Matrix();
            matrix.preScale(1, -1);

            //Create a Bitmap with the flip matrix applied to it.
            //We only want the bottom half of the image
            reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);


            //Create a new bitmap with same width but taller to fit reflection
             bitmapWithReflection = Bitmap.createBitmap(width 
              , (height + height/2), Config.ARGB_8888);

            //Create a new Canvas with the bitmap that's big enough for
            //the image plus gap plus reflection
            Canvas canvas = new Canvas(bitmapWithReflection);
            //Draw in the original image
            canvas.drawBitmap(originalImage, 0, 0, null);
            //Draw in the gap
            Paint deafaultPaint = new Paint();
            canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
            //Draw in the reflection
            canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);

            //Create a shader that is a linear gradient that covers the reflection
            Paint paint = new Paint(); 
            LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, 
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, 
                TileMode.CLAMP); 
            //Set the paint to use this shader (linear gradient)
            paint.setShader(shader); 
            //Set the Transfer mode to be porter duff and destination in
            paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
            //Draw a rectangle using the paint with our linear gradient
                canvas.drawRect(0, height, width, 
                bitmapWithReflection.getHeight() + reflectionGap, paint); 
           ImageView imageView = new ImageView(mContext);
           imageView.setImageBitmap(bitmapWithReflection);
           imageView.setLayoutParams(new CoverFlow.LayoutParams(200, 280));
           imageView.setScaleType(ScaleType.MATRIX);
           mImages[index++] = imageView;
       }catch(Exception e)
       {
           e.printStackTrace();
       }
   }
   return true;
}

您正在加载的图像很可能超过了Android应用程序的RAM限制。加载图像时应使用
inSampleSize
,以便在图像占用所有内存之前将其缩小。我使用此功能加载我的所有图像:

public static Bitmap decodeFile(File file, int size){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inPurgeable = true;
        o.inInputShareable = true;
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file),null,o);

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

        //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<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
公共静态位图解码文件(文件文件,整数大小){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inpurgable=true;
o、 inInputShareable=true;
o、 inJustDecodeBounds=true;
decodeStream(新文件输入流(文件),null,o);
//我们要扩展到的新尺寸
所需最终整型尺寸=尺寸;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2您正在加载的图像很可能超出了Android应用程序的RAM限制。加载图像时,您应该使用
inSampleSize
,以便在图像占用您的所有内存之前将其缩小。我使用此功能加载所有图像:

public static Bitmap decodeFile(File file, int size){
    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inPurgeable = true;
        o.inInputShareable = true;
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file),null,o);

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

        //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<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
公共静态位图解码文件(文件文件,整数大小){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inpurgable=true;
o、 inInputShareable=true;
o、 inJustDecodeBounds=true;
decodeStream(新文件输入流(文件),null,o);
//我们要扩展到的新尺寸
所需最终整型尺寸=尺寸;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){

如果(width_tmp/2Duplicate of?Duplicate of?虽然这可能有助于提高每个位图使用的内存量,但如果分配太多位图,仍然可能会耗尽内存。OP的问题是分配了多少图像。对于使用BitmapFactory.Options.inJustDecodeBounds,+1将比OP的BitmapFactory节省大量内存。Options.inTempStorage=新字节[16*1024];@Daniel您传入的int size是多少?这是图像在调整大小后应放入的像素宽度和高度框。虽然这可能有助于增加每个位图使用的内存量,但如果分配太多位图,您仍然可能会耗尽内存。OP的问题中不清楚分配了多少图像。+1用于使用BitmapFactory.Options.inJustDecodeBounds将比OP的BitmapFactory.Options.inTempStorage=新字节[16*1024];@Daniel传入的int大小是多少?调整大小后,图像应放入像素宽度和高度框中。