我有一个ListView,其元素是我需要从远程API加载的照片,我正在使用loopj来访问。
在loopj下载照片数据后,我使用AsyncTask将数据处理成缩放的位图。
这通常效果很好,但是..
问题
在我的AsyncTask的onPostExecute方法中,我在ImageView上调用setImageBitmap。有时,在调用ImageView.setImageBitmap之后,位图根本不会出现在ImageView中。我已通过日志和调试器验证了ImageView.setImageBitmap确实在有问题的ImageView上调用。
如果我稍微滚动列表,则会重新绘制列表元素,并从内存缓存中加载图像并正确显示在ImageView上。
这是我的代码:
public void setPersonImage(final ImageView personImageView, final Person p) {
personImageView.setImageResource(R.drawable.empty_image);
//kills unecessary requests for the imageview's previous occupant photo
if(personImageView.getTag() != null) {
AsyncHttpClient client = mPhotoRequestsMap.get(p.getEmployeeId());
if(client != null) {
client.cancelRequests(getActivity().getApplicationContext(), true);
Log.d(TAG, "Cancled photo request for " + String.valueOf(p.getEmployeeId()));
}
}
personImageView.setTag(p.getEmployeeId());
//first attempt to grab photo from cache
Bitmap cachedBitmap = mMemoryCache.get(p.getPhotoCacheKey());
if(cachedBitmap != null) {
personImageView.setImageBitmap(cachedBitmap);
Log.d(TAG, "bitmap for " + String.valueOf(p.getEmployeeId()) + " retrieved from memory cache");
} else {
AsyncHttpClient client = ApiWrapper.getPersonImage(180,mUserSession,
getActivity().getApplicationContext(), p.getEmployeeId(),
new MyAsyncHttpResponseHandler(this) {
@Override
public void onSuccess(String response) {
if(personImageView.getTag().equals(p.getEmployeeId())) {
// use async task to process the bitmap off the UI thread
AsyncTask<String,Integer,Bitmap> task = new AsyncTask<String,Integer,Bitmap>() {
@Override
protected Bitmap doInBackground(String... params) {
String response = params[0];
byte[] bitmapBytes = ApiWrapper.processImageResponse(response);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
Bitmap bmpScaled = ThumbnailUtils.extractThumbnail(bitmap, 180, 180);
return bmpScaled;
}
protected void onPostExecute(Bitmap bitmap) {
//set on imageview if tags agree
if(personImageView.getTag().equals(p.getEmployeeId())) {
Log.d(TAG, "setting image view for " + p.getEmployeeId());
personImageView.setImageBitmap(bitmap);
mMemoryCache.put(p.getPhotoCacheKey(), bitmap);
}
else {
Log.d(TAG, "tags don't match after decode bitmap, discarding...");
}
}
};
task.execute(response);
}
else {
Log.d(TAG, "tags don't match BEFORE decode bitmap employee id: " + p.getEmployeeId() + " tag: " + personImageView.getTag());
}
}
}
);
//create entry in PhotoRequestsMap table
mPhotoRequestsMap.put(p.getEmployeeId(), client);
} // end if not in cache
}
我已经尝试在设置位图后调用ImageView上的invalidate(),但遗憾的是没有解决它..
我非常感谢任何帮助或指示。
更新
我的适配器中的getView方法:
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_person, null);
}
Person p = getItem(position);
TextView nameTextView = (TextView)convertView.findViewById(R.id.person_ListItemNameTextView);
nameTextView.setText(p.getFirstName() + " " + p.getLastName());
TextView idTextView = (TextView)convertView.findViewById(R.id.person_ListItemIdTextView);
idTextView.setText(String.valueOf(p.getEmployeeId()));
ImageView personImageView = (ImageView)convertView.findViewById(R.id.person_ListItemImageView);
setPersonImage(personImageView, p);
return convertView;
}
答案 0 :(得分:1)
我认为.setImageBitmap(Bitmap)
方法工作正常,您只需使用null
进行设置。
检查bitmap == null
中onPostExecute(Bitmap bitmap)
的情况。这也适合您在滚动时显示图片的观察结果,因为此时Android会再次为可见行调用getView(..)
。