如何避免内存不足的错误,而在Android中使用位图位图、而在、错误、内存不足

2023-09-12 22:06:35 作者:£君临↘天下彡

我使用位图。它显示了内存不足的错误。能不能避免热。我的code为以下。提前感谢。

 位图MYBITMAP = Image.de codeSampledBitmapFromUri(路,250,500);
img_cook [指数] .setImageBitmap(MYBITMAP);

公共静态位图德codeSampledBitmapFromUr(字符串路径,诠释reqWidth,
            INT reqHeight){

    位图BM = NULL;

    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeFILE(路径选择);
    options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);
    options.inJustDe codeBounds = FALSE;
    BM = BitmapFactory.de codeFILE(路径选择);

    返回BM;
}

公共静态INT calculateInSampleSize(BitmapFactory.Options选项,
        INT reqWidth,诠释reqHeight){
    最终诠释身高= options.outHeight;
    最终诠释宽度= options.outWidth;
    INT inSampleSize = 1;

    如果(高度> reqHeight ||宽度GT; reqWidth){
        如果(宽>高度){
         inSampleSize = Math.round((浮动)的高度/(浮点)reqHeight);
        } 其他 {
         inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
        }
       }
 

解决方案

当你做你的位图,是指当你的位图所做的工作,然后让它recyle和空象下面这样:

  bitmap.recycle();
位= NULL;
 
Android 图片内存优化

我觉得你是从URL中下载图片,所以我建议你使用Android查询了这一点,你将永远不会得到这个错误,如果你使用它。

您可以从这里下载jar文件: HTTP://$c$c.google.com/p/android - 查询/下载/列表 下载jar文件,并设置罐子构建路径。

  AQuery androidAQuery =新AQuery(本);
 

作为一个例子,从URL直接加载图像:

  androidAQuery.id(您ImageView的),在图像配(图像加载,真正的,真实的,getDeviceWidth(),任何默认的图片你想显示);
 

作为一个例子,从URL得到位图:

  androidAQuery.ajax(图像URL,Bitmap.class,0,新AjaxCallback<位图>(){
    @覆盖
    公共无效回调(字符串URL,位图对象,AjaxStatus状态){
        super.callback(URL,对象,地位);

        //你会得到目标位图。
    }
});
 

这是非常快速和准确,并利用这一点,你可以装的时候找到更多的功能,如动画;得到一个位图,如果需要的话;等等。

i am using bitmap. it shows out of memory error. hot can it be avoided. my code is following. advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); 
img_cook[index].setImageBitmap(myBitmap); 

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth,
            int reqHeight) {

    Bitmap bm = null;

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
         inSampleSize = Math.round((float)height / (float)reqHeight);    
        } else {
         inSampleSize = Math.round((float)width / (float)reqWidth);    
        }   
       }

解决方案

When you have done with your Bitmap, means when your Bitmap done its work then make it recyle and null like below:

bitmap.recycle();
bitmap=null;

OR

I think you are downloading Image from url, so I am suggesting you to use Android Query for this, you will never get this error if you used it.

You can download the jar file from here : http://code.google.com/p/android-query/downloads/list Download the jar file and set jar to your Build Path.

 AQuery androidAQuery=new AQuery(this);

As an example to load image directly from url:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

As an example to get Bitmap from url:

androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){
    @Override
    public void callback(String url, Bitmap object, AjaxStatus status) {
        super.callback(url, object, status);

        //You will get Bitmap from object.
    }
});

It's very fast and accurate, and using this you can find many more features like Animation when loading; getting a bitmap, if needed; etc.

 
精彩推荐
图片推荐