我实现了这样的功能:
/** Contains all the pending requests for thumbnails. */
private LinkedList<Uri> mPendingThumbnailRequests = new LinkedList<Uri>();
private ThumbnailGetter mThmGetter = null;
/**
* Asynchronous process for retrieving thumbnails from Uris.
*/
private class ThumbnailGetter extends AsyncTask<Uri, Integer, Uri> {
private final String LOG_TAG = ThumbnailGetter.class
.getSimpleName();
/** The Uri beeing processed */
private Uri mUri = null;
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Uri doInBackground(Uri... uris) {
// We process Uris one after another... so the Array contains
// only one Uri.
mUri = uris[0];
// Let the ThumbnailLoader do the job.
Uri result = ItemsLoader.getThumbnail(mContext, mUri);
return result;
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Uri result) {
super.onPostExecute(result);
// Give the retrieved thumbnail to the adapter...
mImageAdapter.updateThumbUri(mUri, result);
// then process any other pending thumbnail request.
if (!mPendingThumbnailRequests.isEmpty()) {
mThmGetter = new ThumbnailGetter();
mThmGetter.execute(mPendingThumbnailRequests.poll());
}
}
}
然后我使用以下方法添加要加载的uris:
if (!mPendingThumbnailRequests.contains(imageUri)) {
mPendingThumbnailRequests.offer(imageUri);
if (mThmGetter == null
|| mThmGetter.getStatus() == AsyncTask.Status.FINISHED) {
// If the previous instance of the thumbnail getter has
// finished, start a new one.
mHandler.sendEmptyMessage(MSG_SHOW_INDETERMINATE_PROGRESS);
mThmGetter = new ThumbnailGetter();
mThmGetter.execute(mPendingThumbnailRequests.poll());
}
}
这甚至允许您取消请求,使用
mPendingThumbnailRequests.remove()
完整的实现如下:
http://code.google.com/p/emailalbum/source/browse/EmailAlbumAndroid/trunk/src/com/kg/emailalbum/mobile/creator/SelectPictures.java