将图像(作为位图)从sdcard加载到gridview时获取OutOfMemoryError [英] Getting OutOfMemoryError while loading images (as bitmap) from sdcard to gridview

查看:98
本文介绍了将图像(作为位图)从sdcard加载到gridview时获取OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含许多位图及其处理方式的android应用.我经常遇到内存崩溃的情况. 我搜索了很多关于正确和标准的位图加载方法以及我在这里找到的最好的一种方法 有效地加载大型位图

I am developing an android app which contains lot of bitmaps and their processing. I was getting frequent out of memory crashes. I searched a lot for correct and standard way to load bitmaps and the best one I got here Loading Large Bitmaps Efficiently

现在,这降低了崩溃的频率,但是在某些特定区域仍存在一些崩溃,例如,当将存储在SDCARD中的图像显示到gridview时,即使我正在使用异步任务进行加载,我也每次都摆脱了内存崩溃的困扰图片.

Now with this has reduced the frequency of crash but still there are some crashes to some specific area like, while displaying images stored in SDCARD to gridview, I am getting out of memory crash everytime even though I am using async task to load images.

我正在添加代码以及这些问题,请看一下并向我建议代码有什么问题:

I am adding code along with this questions, please have a look and suggest me what is wrong with the code :

LoadImage AsyncTask的PostExecute方法

PostExecute Method of LoadImage AsyncTask

protected void onPostExecute(final Bundle result) {
        super.onPostExecute(result);
        ImageView view = views.get(result.getInt("pos"));
        view.setImageBitmap(BitmapFactory.decodeFile(result.getString("filePath")));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(context, Monsterize.class);
                i.putExtra("flagtwo", 3);
                i.putExtra("backbgpath", filepath);
                i.putExtra("backbgname", fileName);
                i.putExtra("backbgpos", result.getInt("pos"));
                i.putExtra("calledFrom", context.getClass().getSimpleName());
                context.startActivity(i);
            }
        });
    }

然后从网格视图适配器的getview方法调用异步taks

And Async taks is getting called from getview method of grid view adapter

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.gridview_item, null);
    ImageView image = null;
    if (vi != null) {
        image = (ImageView) vi.findViewById(R.id.image);
    }
    Bundle b = new Bundle ();
    b.putString("filePath", filepath[position]);
    b.putInt("pos", position);
    new LoadImage().execute(b);
    views.put(position, image);
    return vi;
}

views是一个哈希图,包含位置作为键,而Imageview作为值.在异步任务中,根据位置在imageview中设置图像.

views is a hashmap containing postion as key and Imageview as value. In async task images get set in imageview based on position.

推荐答案

views.put(position, image);

views是一个哈希图,其中包含位置作为键,而Imageview作为值

views is a hashmap containing postion as key and Imageview as value

这是问题所在.在网格视图管理其视图并重用它们的过程中,以便只有可见的视图正在占用内存,然后将所有图像视图存储在内存中.目前尚不清楚为什么要这么做-在源文件中从未使用views-但它始终引用所有图像.

And here is the problem. While the grid view is managing its views, reusing them, so that only the visible ones are occupying memory, you store all your image views in the memory. It's not clear why you did this - views is never used in your source - but it references all of the images all the time.

这篇关于将图像(作为位图)从sdcard加载到gridview时获取OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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