位图运行时异常中的Outofmemory错误

时间:2012-10-30 14:51:58

标签: android bitmap bitmapimage bitmapfactory

我正在显示来自assests / image文件夹的图片,  但是这段代码不起作用。此代码显示gallery中assets文件夹的图像。我正在使用gallery prefine library或jar文件。

请专家检查一下。谢谢你

AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("image");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }

    for(String filename : files) {
        System.out.println("File name => "+filename);
        InputStream in = null;
        try {
            ImageViewTouch imageView = new ImageViewTouch(Rahul.this); 
            imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
          final Options options = new Options();
            options.outHeight = (int) scaleHeight; 
            options.outWidth = (int) scaleWidth;   
            options.inScaled = true;
            options.inPurgeable = true;
            options.inSampleSize = 2;
           in = assetManager.open("image/"+filename); 
           Bitmap bit=BitmapFactory.decodeStream(in);

          imageView.setImageBitmap(bit);

              } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }
    }
    gallery.setAdapter(arrayAdapter);

2 个答案:

答案 0 :(得分:1)

1)在为图像设置新位图之前,尝试使用bitmap.recycle();释放内存

BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
if (bitmap != null)
{
    bitmap.recycle();
}

2)如果你的图像太大而缩小它们:

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;
    }

<强>更新

类似的东西:

for(int i=0; i<it.size();i++) { 
    ImageViewTouch imageView = new ImageViewTouch(GalleryTouchTestActivity.this); 
    imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    Options options = new Options(); 
    options.inSampleSize = 2;
    String photoURL = it.get(i);

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    if (bitmap != null)
    {
       bitmap.recycle();
    }

    bitmap = BitmapFactory.decodeFile(photoURL);

    imageView.setImageBitmap(bitmap); 
    arrayAdapter.add(imageView);
}

答案 1 :(得分:1)

嘿,请在同一问题上查看我的答案:bitmap size exceeds Vm budget error android

并且在处理这样的位图时也总是尝试使用最大选项:

 final Options options = new Options();
    options.outHeight = (int) scaleHeight; // new smaller height
    options.outWidth = (int) scaleWidth;   // new smaller width
    options.inScaled = true;
    options.inPurgeable = true;

    // to scale the image to 1/8
    options.inSampleSize = 8;
    bitmap = BitmapFactory.decodeFile(imagePath, options);

这可能会解决您的问题。