位图运行时异常OutOfMemory错误 [英] Outofmemory error in bitmap runtime exception

查看:223
本文介绍了位图运行时异常OutOfMemory错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从入资产价值/图像文件夹显示我的图片,
 但这code不工作。从这个资产code显示图像库文件夹。我使用库prefine库或jar文件。

请专家检查。感谢ü

  AssetManager assetManager = getAssets();
    的String []文件= NULL;
    尝试{
        文件= assetManager.list(图像);
    }赶上(IOException异常五){
        Log.e(标签,e.getMessage());
    }    对于(字符串文件名:文件){
        的System.out.println(文件名=>中+文件名);
        在的InputStream = NULL;
        尝试{
            ImageViewTouch的ImageView =新ImageViewTouch(Rahul.this);
            imageView.setLayoutParams(新Gallery.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
          最后的选项选项=新的选项();
            options.outHeight =(INT)scaleHeight;
            options.outWidth =(INT)scaleWidth;
            options.inScaled = TRUE;
            options.inPurgeable = TRUE;
            options.inSampleSize = 2;
           在= assetManager.open(图像/+文件名);
           位图位= BitmapFactory.de codeStream(中);          imageView.setImageBitmap(位);              }赶上(例外五){
            Log.e(标签,e.getMessage());
        }
    }
    gallery.setAdapter(arrayAdapter);


解决方案

1)尽量使用 bitmap.recycle(); 新位图设置之前释放内存你的图片

  BitmapDrawable绘制=(BitmapDrawable)myImage.getDrawable();
位图的位图= drawable.getBitmap();
如果(位图!= NULL)
{
    bitmap.recycle();
}

2)如果你的图片过大比例放倒他们:

 公共静态位图德codeFILE(档案文件,诠释requiredSize){
        尝试{            //德code图像尺寸
            BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
            BitmapFactory.de codeStream(新的FileInputStream(文件),空,O);            //我们希望新的大小缩放到            //找到正确的比例值。它应该是2的幂。
            INT width_tmp = o.outWidth,height_tmp = o.outHeight;
            int标= 1;
            而(真){
                如果(width_tmp / 2'; requiredSize
                        || height_tmp / 2'; requiredSize)
                    打破;
                width_tmp / = 2;
                height_tmp / = 2;
                规模* = 2;
            }            //德code。与inSampleSize
            BitmapFactory.Options O2 =新BitmapFactory.Options();
            o2.inSampleSize =规模;            BMP位图= BitmapFactory.de codeStream(新的FileInputStream(文件),
                    空,O2);            返回BMP;        }赶上(FileNotFoundException异常五){
        } {最后
        }
        返回null;
    }

更新

是这样的:

 的for(int i = 0; I< it.size();我++){
    ImageViewTouch的ImageView =新ImageViewTouch(GalleryTouchTestActivity.this);
    imageView.setLayoutParams(新Gallery.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    选项​​选项=新的选项();
    options.inSampleSize = 2;
    串photoURL = it.get(ⅰ);    BitmapDrawable绘制=(BitmapDrawable)imageView.getDrawable();
    位图的位图= drawable.getBitmap();
    如果(位图!= NULL)
    {
       bitmap.recycle();
    }    位= BitmapFactory.de codeFILE(photoURL);    imageView.setImageBitmap(位图);
    arrayAdapter.add(ImageView的);
}

i am displaying my images from assests/image folder , but this code is not working . this code display images from assets folder in gallery . i am using gallery prefine library or jar file.

please expert check it . thank u

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

解决方案

1) try to use bitmap.recycle(); to release memory before setting a new bitmap to your images

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

2) if your images are too large scale down them:

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

Update

something like this:

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

这篇关于位图运行时异常OutOfMemory错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆