如果您只需要下载几张图片并且不需要显示大图,则可以使用以下代码。它将位图存储在内存中。效果很好是图像不是太大。
我将代码更改为您的情况:
导入android.graphics.Bitmap;
公共类供应商{
// Data
private String mText;
private Bitmap mImage;
private String mImageUrl;
// Flags
private boolean mIsLoading;
public Supplier() {
mText = "test";
mImage = null;
mImageUrl = "image_url";
mIsLoading = false;
}
public Supplier setLoadingStatus(boolean pIsLoading){
mIsLoading = pIsLoading;
return this;
}
public boolean isLoading(){
return mIsLoading;
}
public Supplier setImageUrl(String pImageUrl){
mImageUrl = pImageUrl;
return this;
}
public String getImageUrl(){
return mImageUrl;
}
public Supplier setText(String pText){
mText = pText;
return this;
}
public String getText(){
return mText;
}
public Supplier setImageBitmap(Bitmap bmp){
mImage = bmp;
return this;
}
public Bitmap getImageBitmap(){
return mImage;
}
}
导入 java.io.IOException;导入 java.io.InputStream;导入 java.net.HttpURLConnection;导入 java.net.MalformedURLException;导入 java.net.URL;导入 java.util.ArrayList;
导入android.R;导入android.content.Context;导入android.graphics.Bitmap;导入android.graphics.BitmapFactory;导入android.os.Handler;导入android.os.Message;导入 android.view.LayoutInflater;导入android.view.View;导入android.view.ViewGroup;导入 android.widget.BaseAdapter;导入android.widget.ImageView;导入 android.widget.TextView;
公共类 TestAdapter 扩展 BaseAdapter{
protected static final int MSG_IMAGE_DOWNLOADED = 0;
// Constants
private final String TAG = "TestAdapter";
private ArrayList<Supplier> mItems;
private Context mContext;
private LayoutInflater mLf;
private Handler mHandler;
public TestAdapter(Context pContex) {
mContext = pContex;
mLf = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mItems = new ArrayList<Supplier>();
mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_IMAGE_DOWNLOADED:
if(null != msg.obj){
mItems.get(msg.arg1).setImageBitmap((Bitmap)msg.obj)
.setLoadingStatus(false);
notifyDataSetChanged();
}
break;
default:
break;
}
};
};
}
public TestAdapter addItem(Supplier pItem){
mItems.add(pItem);
return this;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Supplier getItem(int position) {
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(null == convertView){
convertView = mLf.inflate(R.layout.your_resource, parent, false);
vh = new ViewHolder();
vh.mTextView = (TextView)convertView.findViewById(R.id.your_textview_from_resource);
vh.mImage = (ImageView)convertView.findViewById(R.id.yout_imageview_from_resource);
convertView.setTag(vh);
}else{
vh = (ViewHolder)convertView.getTag();
}
vh.mTextView.setText(mItems.get(position).getText());
if(mItems.get(position).getImageBitmap() == null && !mItems.get(position).isLoading()){
// download image
downloadImage(mItems.get(position).getImageUrl(), position);
// set a flag to know that the image is downloading and it is not need to
// start another download if the getView method is called again.
mItems.get(position).setLoadingStatus(true);
}else{
vh.mImage.setImageBitmap(mItems.get(position).getImageBitmap());
}
return null;
}
private void downloadImage(String pImageUrl, int pItemPosition){
final int cItemPosition = pItemPosition;
final String cImageUrl = pImageUrl;
Thread tGetImage = new Thread(new Runnable() {
@Override
public void run() {
Message msg = new Message();
msg.what = MSG_IMAGE_DOWNLOADED;
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bmImg;
URL myFileUrl = null;
try {
myFileUrl= new URL(cImageUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is, null, options);
is.close();
conn.disconnect();
msg.obj = bmImg;
} catch (IOException e) {
e.printStackTrace();
}
msg.arg1 = cItemPosition;
mHandler.sendMessage(msg);
}
});
tGetImage.start();
}
private class ViewHolder{
public TextView mTextView;
public ImageView mImage;
}
}
该代码未经测试,但应该可以工作。