滚动列表视图时内存不足异常? [英] Out of Memory Exception while scroll Listview?

查看:22
本文介绍了滚动列表视图时内存不足异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参考了很多资源,但没有得到正确的答案,

I refered through many resources but can't get right answer,

我制作了一个自定义适配器来在列表视图中查看图像.此图像是从存储卡中检索的.一切运行正常,但是当我滚动列表视图时,我收到 OutOfMemory 异常.我已经发布了用于从 sdcard 检索图像的代码.

I have made an custom adapter to view images in a listview. This images are retrieved from memory card. Everything runs fine but while i scroll through the listview i get an OutOfMemory Exception. I have posting the code using which i retrive images from sdcard.

public void getFromSdcard() {

    File file = new File(
            android.os.Environment.getExternalStorageDirectory(),
            "Tiles/.NoMedia");

    if (file.isDirectory()) {
        listFile = file.listFiles();

        for (int i = 0; i < listFile.length; i++) {

            f.add(listFile[i].getAbsolutePath());

        }
    }
}

这里 f 是字符串的数组列表,我将它传递给自定义适配器,以下是我的自定义适配器的代码.

here f is arraylist of string and i am passing it to an custom adapter following is the code for my custom adapter.

public class NewImageAdapter extends ArrayAdapter<Image> {

private ArrayList<Image> objects;
String packageName;
Activity act;

public NewImageAdapter(Activity context, int image_layout,
        ArrayList<Image> objects) {
    super(context, image_layout, objects);
    this.act = context;
    this.objects = objects;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View v = convertView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }

    Image i = objects.get(position);

    if (i != null) {
        ImageView iv = (ImageView) v.findViewById(R.id.imagemenu123);
        // TextView tv = (TextView) v.findViewById(R.id.commandText);
        if (iv != null) {

            Bitmap myBitmap = BitmapFactory.decodeFile(i.getImagePath());

            iv.setImageBitmap(myBitmap);
            // iv.setImageBitmap(i.getImageBitmap());
            // tv.setText("Tiles Images");
        }

    }
    return v;
}

}

我的问题的任何解决方案:

Any solution to my problem:

推荐答案

使用这个概念会对你有所帮助,然后在图像视图上设置图像位图

use this concept this will help you, After that set the imagebitmap on image view

public static Bitmap convertBitmap(String path)   {

        Bitmap bitmap=null;
        BitmapFactory.Options bfOptions=new BitmapFactory.Options();
        bfOptions.inDither=false;                     //Disable Dithering mode
        bfOptions.inPurgeable=true;                   //Tell to gc that whether it needs free memory, the Bitmap can be cleared
        bfOptions.inInputShareable=true;              //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
        bfOptions.inTempStorage=new byte[32 * 1024]; 


        File file=new File(path);
        FileInputStream fs=null;
        try {
            fs = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            if(fs!=null)
            {
                bitmap=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
            }
            } catch (IOException e) {

            e.printStackTrace();
        } finally{ 
            if(fs!=null) {
                try {
                    fs.close();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        }

        return bitmap;
    }

如果你想从大图像制作一个小图像,高度和宽度像 60 和 60 并快速滚动列表视图,那么使用这个概念

If you want to make a small image from large image with height and width like 60 and 60 and scroll the listview fast then use this concept

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

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        return bmp;
        }

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

希望对你有帮助.

您可以从开发者网站这里

You cam take help from developer site Here

这篇关于滚动列表视图时内存不足异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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