Androidクイック開発フレームワークvolleyで発生した問題

1953 ワード

volleyを使っている人はOOMのエラーに遭遇したことがあるに違いありません.ここ数日、NetWorkImageViewを使って大量の画像のダウンロードサンプリングを行いました.GridViewにバインドされているのにOOM異常が発生し、その後多くの資料を検索し、volleyソースコードを見て分析し、RequestQueueオブジェクトを固定して、単利モードで実現したらどうなるか大胆に推測した.volleyのnewRequestQueueメソッドでRequestQueue=new RequestQueue(new DiskBasedCache(cacheDir)、networkを見たからです.
だからVolleyクラスを書き直しました
public class MyVolley extends Volley {

	private static RequestQueue rq = null;

	public synchronized static RequestQueue getInstance(Context context) {
		if (rq == null) {
			rq = Volley.newRequestQueue(context);
		}
		return rq;
	}

}

そしてMyVolleyを利用してリクエストキューを取得しImageLoaderの実装を行う
public class MyImageLoader extends ImageLoader {

	private MyImageLoader(RequestQueue queue, ImageCache imageCache) {
		super(queue, imageCache);
	}

	public MyImageLoader(Context context) throws Exception {
		super(MyVolley.getInstance(context), new BitMapCache(context));
	}
}

その後BitmapCacheキャッシュを実現
public class BitMapCache implements ImageCache {
	//      
	private Context context = null;

	//     SD        
	public static final int SD_MAX_CACHE_SIZE = 15;

	/**
	 *     
	 */
	public BitMapCache(Context context) {
		MySQLiteOpenHelper ms = new MySQLiteOpenHelper(context);
		SQLiteUtils.deleteCache(ms);
		this.context = context;
	}

	/**
	 *          
	 */
	@Override
	public Bitmap getBitmap(String url) {
		return MyLruCache.getBitMap(context, url);
	}

	/**
	 *            
	 */
	@Override
	public void putBitmap(String key, Bitmap value) {
		new MyThread(context, key, value).start();
	}

	class MyThread extends Thread {

		private Context context = null;
		private String key = null;
		private Bitmap bitmap = null;

		public MyThread(Context context, String key, Bitmap value) {
			this.context = context;
			this.key = key;
			this.bitmap = value;
		}

		@Override
		public void run() {
			MyLruCache.putBitmap(context, key, bitmap);
		}
	}

}