我的 android listview 图片 内存溢出 解决方案



刚开始接触android开发的时候 经常会碰到一个问题 就是 listview 里的图片太多 会导致 listview 的OutOfMemoryException发生,

而网上却没有很详细的解决方案,只有例如 软引用 ,手动recycle 资源,缩小bitmap等等。(不过貌似都治标不治本,所以以前这个问题 一直困扰了我很久。。。)


即使使用了这些解决方案 也很可能碰到 以下的几个问题


1. 图片 比如 bitmap 或者 drawable 虽然可以用recycle 方法手动释放,但是 释放的时机。

2. 即使使用手动释放,但由于 图片被 imageview 或者其他控件引用 导致发生异常 比如 trying to use a recycled bitmap



我比较傻瓜的解决方案是


testmemoryadapter.java

package com.testmemoryadapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class TestAdapter extends BaseAdapter {

	private ArrayList list;
	private LayoutInflater mInflater;
	private Context context;
	//这个用来保存 imageview 的引用
	private ArrayList viewList = new ArrayList();
	//这个用来 保存 bitmap
	private ArrayList bitmapList = new ArrayList();

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public TestAdapter(Context context, ArrayList list) {
		super();
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.list = list;
	}

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

		convertView = mInflater.inflate(R.layout.test_list_row, null);

		ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
		
		
		//用try catch 块包围住
		try {
			setImage(iv);
		} catch (OutOfMemoryError e) {
			// 这里就是当内存泄露时 需要做的事情
			e.printStackTrace();

			Log.d("memory", "out");
			
			//释放内存资源
			recycleMemory();
			
			//将刚才 发生异常没有执行的 代码 再重新执行一次
			setImage(iv);

		}

		return convertView;
	}

	
	//这里是关键
	private void recycleMemory() {
		//一屏显示多少行 这里就设置为多少。不设也行 主要是用户体验好 不会将用户看到的图片设为默认图片
		int showCount = 10;
		
		//
		for (int i = 0; i < viewList.size()-showCount; i++) {
			ImageView iv = (ImageView) viewList.get(i);
			/***
			 *  这里是关键! 将 imageview 设置一张默认的图片 ,
			 *  用于解决当释放bitmap的时候 还有其他 控件对他保持引用
			 *  就不会发生trying to use a recycled bitmap异常了
			 */
			iv.setImageResource(R.drawable.default_cover);
			//从list中去除
			viewList.remove(i);
		}

//		viewList = new ArrayList();

		for (int i = 0; i < bitmapList.size()-10; i++) {

			Bitmap bitmap = (Bitmap) bitmapList.get(i);
			//这里就开始释放bitmap 所占的内存了
			if (!bitmap.isRecycled()) {
				bitmap.recycle();
				System.out.println("recycle ");
			}
			//从list中去除
			bitmapList.remove(i);
		}

//		bitmapList = new ArrayList();
	}
	
	private void setImage(ImageView iv){
		/***
		 * 从sdcard获取 图片  这张图片 只要不超过  android对于图片大小的限制即可 
		 * 我用了 一张比较大的图片 也通过测试
		 */
		Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test/1.jpg");

		iv.setImageBitmap(bitmap);
		
		//将这个控件 添加到 list里
		viewList.add(iv);
		//将要 释放的 bitmap也添加到list里
		bitmapList.add(bitmap);
	}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dip"
        android:layout_height="80dip" android:src="@drawable/default_cover"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/testListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

package com.testmemoryadapter;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList testList = new ArrayList();
        
        for (int i = 0; i < 30; i++) {
			testList.add(0);
		}
        
        TestAdapter ta = new TestAdapter(this,testList);
        
        ListView lv = (ListView) findViewById(R.id.testListView);
        
        lv.setAdapter(ta);
        
    }
}


接下来放心大胆的测试吧吗哈哈 ,这个解决方案 虽然并不是很规范 但是基本能解决 内存溢出的问题。我用了500k左右的图片 测试下没问题 还有我的运行版本是2.2。希望能给被这个问题困扰的朋友们提供些思路。


http://download.csdn.net/detail/w237or45/4124069

这里是源码下载的位置


用源码里的图片在普通情况下显示10张就 溢出了 ,用了以下的方法后 测试200张 都没有问题~





  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 22
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值