80,351
社区成员
发帖
与我相关
我的任务
分享
package com.hjl.topview2.adapter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.TextView;
import com.hjl.topview2.R;
import com.hjl.topview2.bean.Project;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class ProjectAdapter extends CommonAdapter<Project>{
/**
* 图片缓存技术的核心类,用于缓存所有下载好的图片,在程序内存达到设定值时会将最少最近使用的图片移除掉。
*/
private LruCache<String, BitmapDrawable> mMemoryCache;
public ProjectAdapter(List<Project> list) {
super(list);
}
@Override
public int setLayoutRes() {
return R.layout.item_project;
}
@Override
public void setData(ViewHolder holder, final Project project) {
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, BitmapDrawable>(cacheSize) {
@Override
protected int sizeOf(String key, BitmapDrawable drawable) {
return drawable.getBitmap().getByteCount();
}
};
TextView TvTitle = holder.getView(R.id.project_tv_title);
TextView TvDesc = holder.getView(R.id.project_tv_desc);
TextView TvDate = holder.getView(R.id.project_tv_date);
TextView TvAuthor = holder.getView(R.id.project_tv_author);
ImageView image = holder.getView(R.id.project_image);
image.setImageResource(R.drawable.empty_photo);
image.setTag(project.getEnvelopePic());
BitmapDrawable drawable = getBitmapFromMemoryCache(project.getEnvelopePic());
if (drawable != null) {
image.setImageDrawable(drawable);
} else {
BitmapWorkerTask task = new BitmapWorkerTask();
task.execute(project.getEnvelopePic());
}
TvTitle.setText(project.getTitle());
TvDesc.setText(project.getDesc());
TvDate.setText(project.getNiceDate());
TvAuthor.setText(project.getAuthor());
}
public void addBitmapToMemoryCache(String key, BitmapDrawable drawable) {
if (getBitmapFromMemoryCache(key) == null) {
mMemoryCache.put(key, drawable);
}
}
public BitmapDrawable getBitmapFromMemoryCache(String key) {
return mMemoryCache.get(key);
}
class BitmapWorkerTask extends AsyncTask<String, Void, BitmapDrawable> {
String imageUrl;
@Override
protected BitmapDrawable doInBackground(String... strings) {
imageUrl = strings[0];
Bitmap bitmap = downloadBitmap(imageUrl);
BitmapDrawable drawable = new BitmapDrawable(mListView.getResources(), bitmap);
addBitmapToMemoryCache(imageUrl, drawable);
return drawable;
}
@Override
protected void onPostExecute(BitmapDrawable drawable) {
ImageView imageView = mListView.findViewWithTag(imageUrl);
if (imageView != null && drawable != null) {
imageView.setImageDrawable(drawable);
}
}
private Bitmap downloadBitmap(String imageUrl) {
Bitmap bitmap = null;
HttpURLConnection con = null;
try {
URL url = new URL(imageUrl);
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(8000);
con.setReadTimeout(8000);
bitmap = BitmapFactory.decodeStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
}
return bitmap;
}
}
}
2019-05-14 16:11:46.863 2018-2045/com.hjl.topview2 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.hjl.topview2, PID: 2018
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getByteCount()' on a null object reference
at com.hjl.topview2.adapter.ProjectAdapter$1.sizeOf(ProjectAdapter.java:42)
at com.hjl.topview2.adapter.ProjectAdapter$1.sizeOf(ProjectAdapter.java:39)
at android.support.v4.util.LruCache.safeSizeOf(LruCache.java:259)
at android.support.v4.util.LruCache.put(LruCache.java:145)
at com.hjl.topview2.adapter.ProjectAdapter.addBitmapToMemoryCache(ProjectAdapter.java:68)
at com.hjl.topview2.adapter.ProjectAdapter$BitmapWorkerTask.doInBackground(ProjectAdapter.java:85)
at com.hjl.topview2.adapter.ProjectAdapter$BitmapWorkerTask.doInBackground(ProjectAdapter.java:76)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, BitmapDrawable>(cacheSize) {
@Override
protected int sizeOf(String key, BitmapDrawable drawable) {
return drawable.getBitmap().getByteCount();
}
};
然后需要判断加载失败的情况
@Override
protected Drawable doInBackground(String... strings) {
imageUrl = strings[0];
Bitmap bitmap = downloadBitmap(imageUrl);
// 如果为null 显示加载失败的图片
if( bitmap == null){
return ContextCompat.getDrawable(context, R.drawable.error_image);
}
BitmapDrawable drawable = new BitmapDrawable(mListView.getResources(), bitmap);
addBitmapToMemoryCache(imageUrl, drawable);
return drawable;
}
@Override
protected void onPostExecute(Drawable drawable) {
ImageView imageView = mListView.findViewWithTag(imageUrl);
if (imageView != null && drawable != null) {
imageView.setImageDrawable(drawable);
}
}
加载过慢可以考虑先显示默认图,加载慢的原因有图片过大,网络慢等等。