用ImageLoder加载图片
程序员文章站
2024-03-17 11:07:22
...
注意了 : 千万不要忘了给ImageLoder初始化 还有不要忘了倒包
public class ImageLoder extends AsyncTask<String,Void,Bitmap>{
public interface ImageBall{
void callBitmap(Bitmap bitmap);
}
private ImageBall imageBall;
public ImageLoder(ImageBall imageBall) {
this.imageBall = imageBall;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
imageBall.callBitmap(bitmap);
}
@Override
protected Bitmap doInBackground(String... params) {
try {
String str= params[0];
URL url=new URL(str);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int code= connection.getResponseCode();
if (code==200){
InputStream si= connection.getInputStream();
return BitmapFactory.decodeStream(si);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}