私のアプリケーションは、Web からデータ (画像とテキスト) をダウンロードし、ListView として表示します。ListView アダプターでは、ビットマップを格納するためのメモリ キャッシュを実装しました。ListView から画像を取得する主な目的は、行をクリックした後に新しいアクティビティに渡すことです。イメージ URL を新しいアクティビティに渡してから再度ダウンロードできることはわかっていますが、そのようなことはしたくありません。ListView からビットマップを取得することは可能ですか? はいの場合、実装が最も効率的で簡単な方法はどれですか? 前もって感謝します
編集:
public class CinemasAdapter extends ArrayAdapter<Cinemas> {
private ArrayList<Cinemas> movieDataItems;
private Activity context;
private LinkedHashMap<String, Bitmap> bitmapCache = new LinkedHashMap<String, Bitmap>();
public CinemasAdapter(Activity context, int textViewResourceId, ArrayList<Cinemas> movieDataItems) {
super(context, textViewResourceId, movieDataItems);
this.context = context;
this.movieDataItems = movieDataItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.movie_data_row, null);
holder.poster = (ImageView) convertView.findViewById(R.id.movie_thumb_icon);
holder.bitmap = BitmapFactory.decodeResource(holder.poster.getResources(), R.drawable.no_image);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
Cinemas movie = movieDataItems.get(position);
if (movie!=null){
String url = movie.poster();
if (url != null) {
Bitmap bitmap = fetchBitmapFromCache(url);
if (bitmap == null) {
loadBitmap(url,holder.poster,holder.bitmap);
}
else {
holder.poster.setImageBitmap(bitmap);
}
}
else {
holder.poster.setImageResource(R.drawable.no_image);
}
}
else {
holder.poster.setImageResource(R.drawable.no_image);
}
return convertView;
}
private void addBitmapToCache(String url, Bitmap bitmap) {
if (bitmap != null) {
synchronized (bitmapCache) {
bitmapCache.put(url, bitmap);
}
}
}
private Bitmap fetchBitmapFromCache(String url) {
synchronized (bitmapCache) {
final Bitmap bitmap = bitmapCache.get(url);
if (bitmap != null) {
bitmapCache.remove(url);
bitmapCache.put(url, bitmap);
return bitmap;
}
}
return null;
}
private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private String url;
private final WeakReference<ImageView> imageViewReference;
private Cinemas movie;
public BitmapDownloaderTask(ImageView imageView, Cinemas mov) {
imageViewReference = new WeakReference<ImageView>(imageView);
movie = mov;
}
@Override
protected Bitmap doInBackground (String... source) {
url = source[0];
Bitmap image;
try{
image = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
return image;
}
catch(Exception e){Log.e("Error", e.getMessage()); e.printStackTrace();}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
final BitmapDownloaderTask bitmapWorkerTask =
getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
addBitmapToCache(url);
movie.setBitmap(bitmap);
}
}
}
}
private static class ViewHolder{
ImageView poster;
Bitmap bitmap;
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapDownloaderTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap,
BitmapDownloaderTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<BitmapDownloaderTask>(bitmapWorkerTask);
}
public BitmapDownloaderTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
private static BitmapDownloaderTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
public static boolean cancelPotentialWork(String data, ImageView imageView) {
final BitmapDownloaderTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final String bitmapData = bitmapWorkerTask.url;
if (bitmapData != data) {
bitmapWorkerTask.cancel(true);
} else {
return false;
}
}
return true;
}
public void loadBitmap(String resId, ImageView imageView, Bitmap bitmap) {
if (cancelPotentialWork(resId, imageView)) {
final BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(imageView.getResources(), bitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(resId);
}
}
}