圖片不顯示
package com.fajiuzhishan.thread;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ImageLoader {
? ?private ImageView mImageView;
? ?private String mUrl;
? ?private Handler mHandler = new Handler() {
? ? ? ?@Override
? ? ? ?public void handleMessage(Message msg) {
? ? ? ? ? ?super.handleMessage(msg);
? ? ? ? ? ?if (mImageView.getTag().equals(mUrl))
? ? ? ? ? ? ? ?mImageView.setImageBitmap((Bitmap) msg.obj);
? ? ? ?}
? ?};
? ?public void ShowImageByThread(ImageView imageView, final String url) {
? ? ? ?mImageView = imageView;
? ? ? ?mUrl = url;
? ? ? ?new Thread() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void run() {
? ? ? ? ? ? ? ?super.run();
? ? ? ? ? ? ? ?Bitmap bitmap = getBtimapFromURL(url);
? ? ? ? ? ? ? ?Message message = Message.obtain();
? ? ? ? ? ? ? ?message.obj = bitmap;
? ? ? ? ? ? ? ?mHandler.sendMessage(message);
? ? ? ? ? ?}
? ? ? ?}.start();
? ?}
? ?public Bitmap getBtimapFromURL(String urlString) {
? ? ? ?Bitmap bitmap;
? ? ? ?InputStream is = null;
? ? ? ?try {
? ? ? ? ? ?URL url = new URL(urlString);
? ? ? ? ? ?HttpURLConnection connection = (HttpURLConnection) url.openConnection();
? ? ? ? ? ?is = new BufferedInputStream(connection.getInputStream());
? ? ? ? ? ?bitmap = BitmapFactory.decodeStream(is);
? ? ? ? ? ?connection.disconnect();
? ? ? ? ? ?return bitmap;
? ? ? ?} catch (MalformedURLException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?} finally {
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?is.close();
? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?}
? ? ? ? ? ?return null;
? ? ? ?}
? ?}
? ?public void showImageByAsyncTask(ImageView imageView,String url){
? ? ? ? ;new NewsAsyncTask(imageView).execute(url);
? ?}
? ? ? ?private class NewsAsyncTask extends AsyncTask<String,Void,Bitmap>{
? ? ? ? ? ?private ImageView mImageView;
? ? ? ? ? ?public NewsAsyncTask(ImageView imageView) {
? ? ? ? ? ? ? ?this.mImageView = imageView;
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?protected Bitmap doInBackground(String... strings) {
? ? ? ? ? ? ? ?return getBtimapFromURL(strings[0]);
? ? ? ? ? ?}
? ? ? ? ? ?@Override
? ? ? ? ? ?protected void onPostExecute(Bitmap bitmap) {
? ? ? ? ? ? ? ?super.onPostExecute(bitmap);
? ? ? ? ? ? ? ?mImageView.setImageBitmap(bitmap);
? ? ? ? ? ?}
}
在Adapter中 用了
new ImageLoader().showImageByAsyncTask(viewHolder.ig_view,url);
?debug 時 ?顯示Bitmap 為空。
2016-08-05
getBtimapFromURL()方法中去掉connection.disconnect();這句看看,
然后在Adapter中
new ImageLoader().showImageByAsyncTask(viewHolder.ig_view.mList.get(position),url);
適配器輔助類中定義private List<ItemBean> mList;
并加入到構造函數public ItemAdapter(Context context,List<ItemBean> mList) {
? ?this.mList = mList;
? ?mInflater=LayoutInflater.from(context);
}
2016-08-05
getBtimapFromURL()方法中去掉connection.disconnect();這句看看