zoukankan      html  css  js  c++  java
  • Android 异步从网络上下载图片

    package com.example.android_asynctask;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    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.AsyncTask;
    import android.os.Bundle;
    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 Button button;
        private ImageView imageView;
        private String image_Path = "http://e.hiphotos.baidu.com/image/pic/item/2fdda3cc7cd98d10b510fdea233fb80e7aec9021.jpg";
        private ProgressDialog dialog;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button = (Button) findViewById(R.id.button1);
            imageView = (ImageView) findViewById(R.id.imageView1);
            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 MYTask().execute(image_Path);
                }
            });
        }
    
        /**
         * 使用异步任务的规则: 1、申明的类继承AsyncTask 标注三个参数的类型
         * 2、第一个参数表示要执行的任务,通常是网络的路径;第二个参数表示进度的刻度,第三个参数表示任务执行的返回结果
         * 
         * @author liende
         * 
         */
        public class MYTask extends AsyncTask<String, Void, Bitmap> {
            /**
             * 表示任务执行之前的操作
             */
            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                dialog.show();
            }
    
            /**
             * 主要是完成耗时的操作
             */
            @Override
            protected Bitmap doInBackground(String... arg0) {
                // TODO Auto-generated method stub
                // 使用网络连接类HttpClient类王城对网络数据的提取
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(arg0[0]);
                Bitmap bitmap = null;
                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    if (httpResponse.getStatusLine().getStatusCode() == 200) {
                        HttpEntity httpEntity = httpResponse.getEntity();
                        byte[] data = EntityUtils.toByteArray(httpEntity);
                        bitmap = BitmapFactory
                                .decodeByteArray(data, 0, data.length);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
                return bitmap;
            }
    
            /**
             * 主要是更新UI的操作
             */
            @Override
            protected void onPostExecute(Bitmap result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
                imageView.setImageBitmap(result);
                dialog.dismiss();
            }
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }


    <uses-permission android:name="android.permission.INTERNET" />
     
    Android 成长之路
  • 相关阅读:
    数据结构之整数划分问题(转)
    各种排序方法的收集
    bloom filter 的Java 版
    常见面试题学习(3)
    优先级队列的Java ,C++ STL,堆实现
    常见面试题学习(2)
    常见面试题学习(5)
    常见面试题学习(4)
    常见面试题学习(1)
    bitmap与桶方式对1000万数据进行排序(转+自己实现理解)
  • 原文地址:https://www.cnblogs.com/liende/p/3909010.html
Copyright © 2011-2022 走看看