Android 如何克服此错误:java.lang.OutOfMemoryError:位图大小超出VM预算

Android 如何克服此错误:java.lang.OutOfMemoryError:位图大小超出VM预算,android,Android,我正在尝试在Sqlite DB中添加图像,并将图像从DB列表到listview。。。。我正在存储图像路径以获取图像。 当我在设备中列出DB中的图像时,我得到了类似java.lang.OutOfMemoryError的错误:位图大小超过VM预算 我是否每次都清除堆内存。 如何纠正这一点。。这是我的密码 LView.class mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToWrite();

我正在尝试在Sqlite DB中添加图像,并将图像从DB列表到listview。。。。我正在存储图像路径以获取图像。 当我在设备中列出DB中的图像时,我得到了类似
java.lang.OutOfMemoryError的错误:位图大小超过VM预算

我是否每次都清除堆内存。 如何纠正这一点。。这是我的密码

LView.class

mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();                
mAlbum = (ImageView) findViewById(R.id.iv);
mAlbum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, IMG);
}
});

Button click = (Button) findViewById(R.id.button);
click.setOnClickListener(new OnClickListener() {
@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        mySQLiteAdapter.insert(str);
        System.out.println(str+" Added");
        Intent intent = new Intent(LView.this, MyList.class);
        startActivity(intent);              
    }
});`
`

请建议我。。。 提前谢谢。

请看这个问题


您是否尝试过使用一个类范围的位图并在您的case子句中重新分配它?

1)尝试先解码绑定的图像 您可以获得图像的实际大小以防止OOM问题。永远不要先解码图像

    BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);'
options.outWidth和options.outHeight是您想要的

2)计算样本量 通过使用来自的以下代码。如果您检测到outWidth和outHeight太大而没有OOM问题,只需将outWidth和outHeight设置为较小的尺寸。它将为您提供一个样本大小,解码后的图像将被设置为向外宽度和向外高度。此处的位图选项与步骤1中使用的相同

    private static int computeInitialSampleSize(BitmapFactory.Options options,
        int minSideLength, int maxNumOfPixels) {
    double w = options.outWidth;
    double h = options.outHeight;

    int lowerBound = (maxNumOfPixels == IImage.UNCONSTRAINED) ? 1 :
            (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
    int upperBound = (minSideLength == IImage.UNCONSTRAINED) ? 128 :
            (int) Math.min(Math.floor(w / minSideLength),
            Math.floor(h / minSideLength));

    if (upperBound < lowerBound) {
        // return the larger one when there is no overlapping zone.
        return lowerBound;
    }

    if ((maxNumOfPixels == IImage.UNCONSTRAINED) &&
            (minSideLength == IImage.UNCONSTRAINED)) {
        return 1;
    } else if (minSideLength == IImage.UNCONSTRAINED) {
        return lowerBound;
    } else {
        return upperBound;
    }
}
private static int computeInitialSampleSize(BitmapFactory.Options,
int minSideLength,int maxnumof像素){
双w=选项。向外宽度;
双h=选项。外高度;
int lowerBound=(maxNumOfPixels==IImage.unconstraint)?1:
(int)Math.ceil(Math.sqrt(w*h/maxNumOfPixels));
int上限=(minSideLength==IImage.unconstraint)?128:
(内部)数学最小值(数学楼层(w/minSideLength),
数学地板(h/分钟长度);
if(上界<下界){
//当没有重叠区域时,返回较大的区域。
返回下边界;
}
if((maxNumOfPixels==IImage.unconstraint)&&
(minSideLength==IImage.unconstraint)){
返回1;
}else if(minSideLength==IImage.unconstraint){
返回下边界;
}否则{
返回上限;
}
}
3) 使用计算出的样本量 获得样本大小后,使用它对真实图像数据进行解码

            options.inTempStorage = new byte[16*1024];
        options.inPreferredConfig = (config == null)?BitmapUtil.DEFAULT_CONFIG:config;
        options.inSampleSize = BitmapUtil.computeSampleSize(bitmapWidth, bitmapHeight, bitmapWidth < bitmapHeight?targetHeight:targetWidth, bitmapWidth < bitmapHeight?targetWidth:targetHeight, 1);
        options.inPurgeable = true;  
        options.inInputShareable = true;  
        options.inJustDecodeBounds = false;
        options.inDither = true;
        result = BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);
options.inTempStorage=新字节[16*1024];
options.inPreferredConfig=(config==null)?BitmapUtil.DEFAULT\u config:config;
options.inSampleSize=BitmapUtil.computeSampleSize(bitmapWidth、bitmapHeight、bitmapWidth
~z~如果这仍然会给您带来OOM问题,您可以尝试降低外径和外径。 位图使用的是本机堆,而不是java堆。因此,很难检测解码后的新图像还剩下多少内存


~~如果必须将outWidth和outHeight设置得太低,那么代码中可能存在内存泄漏。尝试从内存中释放任何你不使用的对象。
例如bitmap.release()



~~~以上只是一个示例代码。调整您需要的。

嗨,不,不。。。。我仍然有问题。。。如果问题解决了,请投你一票。。。抱歉耽搁了-Polamreddy虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效。
            options.inTempStorage = new byte[16*1024];
        options.inPreferredConfig = (config == null)?BitmapUtil.DEFAULT_CONFIG:config;
        options.inSampleSize = BitmapUtil.computeSampleSize(bitmapWidth, bitmapHeight, bitmapWidth < bitmapHeight?targetHeight:targetWidth, bitmapWidth < bitmapHeight?targetWidth:targetHeight, 1);
        options.inPurgeable = true;  
        options.inInputShareable = true;  
        options.inJustDecodeBounds = false;
        options.inDither = true;
        result = BitmapFactory.decodeByteArray(bitmapByteArray, 0, bitmapByteArray.length, options);