亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

圖片不顯示

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 為空。

正在回答

2 回答

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);
}


0 回復 有任何疑惑可以回復我~

getBtimapFromURL()方法中去掉connection.disconnect();這句看看

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android必學-異步加載
  • 參與學習       50616    人
  • 解答問題       333    個

了解Android中的異步加載處理方法,這是面試問的最多的知識點

進入課程

圖片不顯示

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號