数组越界异常 求解决!!!
package com.example.imooc;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
public class ImageLoader {
private ImageView mImageView;
private String murl;
private LruCache<String, Bitmap> mCaches;
private ListView mListView;
private Set<MyAsyncTask> mTask;
public ImageLoader(ListView listView) {
mListView=listView;
mTask=new HashSet<ImageLoader.MyAsyncTask>();
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 4;
mCaches = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
// 在每次存入缓存的时候调用
return value.getByteCount();
}
};
}
// 添加到缓存,put()
public void addBitmapToCache(String url, Bitmap bitmap) {
if (getBitmapFromCache(url) == null) {
mCaches.put(url, bitmap);
}
}
// 缓存中提取数据 get()
public Bitmap getBitmapFromCache(String url) {
return mCaches.get(url);
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mImageView.getTag().equals(murl)) {
mImageView.setImageBitmap((Bitmap) msg.obj);
}
}
};
public void showImageByThread(ImageView imageView, final String url) {
mImageView = imageView;
murl = url;
new Thread() {
@Override
public void run() {
super.run();
Bitmap bitmap = getBitmapFromURL(url);
Message message = Message.obtain();
message.obj = bitmap;
mHandler.sendMessage(message);
}
}.start();
}
public Bitmap getBitmapFromURL(String urlString) {
Bitmap bitmap;
InputStream is = null;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
// 这个忘记return bitmap后 message无法打包bitmap 导致程序崩溃!!。
return bitmap;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void showImageByAsyncTask(ImageView imageView, String url) {
// 从缓存中提取图片
Bitmap bitmap = getBitmapFromCache(url);
// 如果没有,就到网络中下载。如果有,直接提取。
if (bitmap == null) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageBitmap(bitmap);
}
}
public void cancelAllTasks(){
if (mTask!=null) {
for (MyAsyncTask task: mTask) {
task.cancel(false);
}
}
}
public void loadImages(int start,int end){
for (int i = start; i < end; i++) {
String url =MyAdapter.URLS[i];
// 从缓存中提取图片
Bitmap bitmap = getBitmapFromCache(url);
// 如果没有,就到网络中下载。如果有,直接提取。
if (bitmap == null) {
MyAsyncTask task=new MyAsyncTask(url);
task.execute();
mTask.add(task);
} else {
ImageView imageView=(ImageView) mListView.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
public class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
// private ImageView mImageView;
private String murl;
public MyAsyncTask(String url) {
// mImageView = imageView;
murl = url;
}
@Override
protected Bitmap doInBackground(String... arg0) {
String url = arg0[0];
// 获取图片,将图片保存到cache;
Bitmap bitmap = getBitmapFromURL(url);
if (bitmap !=null) {
// 将不在缓存的图片加入缓存;
addBitmapToCache(url, bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
ImageView imageView=(ImageView) mListView.findViewWithTag(murl);
if (imageView!=null && result!=null) {
imageView.setImageBitmap(result);
}
}
}
}