数枚の画像をダウンロードする必要があり、大きな画像を表示する必要がない場合は、次のコードを使用できます。ビットマップをメモリに保存します。画像が大きすぎないのでうまく機能します。
あなたの状況に合わせてコードを変更しました:
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;
}
}
import java.io.IOException; java.io.InputStream をインポートします。java.net.HttpURLConnection をインポートします。java.net.MalformedURLException をインポートします。java.net.URL をインポートします。import 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 をインポートします。
public class 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;
}
}
コードはテストされていませんが、動作するはずです。