[Android]结合Handler下载图片

布局文件, 一个Button开启点击事件, 一个ImageView用来显示所下载的图片

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <Button
        android:id="@+id/button" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
    <ImageView
        android:id="@+id/imageView" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

然后是Activity页面代码

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.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.activity001.R;

public class DownloadImageActivity extends Activity {
    
    private Button button;
    private ImageView imageView;
    private final static String IMAGE_URL = "http://d.hiphotos.bdimg.com/album/s%3D1000%3Bq%3D90/sign=00478c8fc9ea15ce45eee40986300182/dcc451da81cb39dbb581855cd0160924ab183072.jpg"; 
    private ProgressDialog progressDialog = null;
    
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            if(msg != null) {
                byte[] data = (byte[]) msg.obj;
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                imageView.setImageBitmap(bitmap);
                if(msg.what == 1) {
                    progressDialog.dismiss();
                    Toast.makeText(DownloadImageActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
                    button.setVisibility(View.INVISIBLE);
                }
            }
        }
        
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_image);
        
        button = (Button) findViewById(R.id.button);
        button.setText("点击下载图片");
        imageView = (ImageView) findViewById(R.id.imageView);
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("提示");
        progressDialog.setMessage("正在下载, 请等待...");
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                progressDialog.show();
                new Thread(new MyThread()).start();
            }
        });
    }
    
    private class MyThread implements Runnable {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(IMAGE_URL);
            try {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                HttpResponse resp = httpClient.execute(httpGet);
                
                int respCode = resp.getStatusLine().getStatusCode();
                
                if(respCode == 200) {
                    byte[] date = EntityUtils.toByteArray(resp.getEntity());
                    Message msg = Message.obtain();
                    msg.obj = date;
                    msg.what = 1;
                    handler.sendMessage(msg);
                }
                
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

运行效果图:

   

转载于:https://www.cnblogs.com/wisher/p/3264773.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值