91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android多線程——Handler (一) 實現圖片下載

發布時間:2020-06-27 15:10:05 來源:網絡 閱讀:1131 作者:just2012xia 欄目:移動開發

在UI線程中創建子線程——》在子線程中獲取網絡圖片--》在線程中通過Message 傳遞二進制圖片給Handler,--》Handler在handleMessage()中處理消息


package com.example.android_handler_message;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
    private static int IS_FINISHED = 1;
    private Button button = null;
    private ImageView p_w_picpathView = null;
    private String p_w_picpath_path = "http://i1.juyouqu.com/uploads/content//2014/04/1396611698344.jpg!w598";
    private ProgressDialog dialog = null;
    private Handler handle = new Handler(){
        public void handleMessage(android.os.Message msg)
        {
            Bitmap bitmap = null;
            byte[] data = (byte[])msg.obj;
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            if(msg.what == IS_FINISHED)
            {
                dialog.dismiss();
                p_w_picpathView.setImageBitmap(bitmap);
            }
        }
          
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        button = (Button)findViewById(R.id.button1);
        p_w_picpathView = (ImageView)findViewById(R.id.p_w_picpathView1);
        dialog = new ProgressDialog(this);
        dialog.setTitle("提示");
        dialog.setMessage("正在下載..........");
          
          
        button.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                new Thread(new MyTask()).start();
                dialog.show();
            }
        });
    }
    public class MyTask implements Runnable
    {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(p_w_picpath_path);
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                if(httpResponse.getStatusLine().getStatusCode() == 200)
                {
                    byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
                    Message message = Message.obtain();
                    message.obj = data;
                    message.what = IS_FINISHED;
                    handle.sendMessage(message);
                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
          
    }
}


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

灯塔市| 博兴县| 北票市| 石台县| 荔浦县| 巴南区| 常宁市| 定南县| 出国| 延长县| 仙桃市| 石家庄市| 永靖县| 马尔康县| 朝阳县| 梅州市| 山丹县| 鲁山县| 中宁县| 高邑县| 扶沟县| 佛冈县| 永宁县| 彩票| 视频| 昌平区| 逊克县| 临汾市| 平泉县| 和林格尔县| 兴文县| 垦利县| 江西省| 建昌县| 大化| 白玉县| 三明市| 灵石县| 剑川县| 封丘县| 拉孜县|