AsyncTask的使用 (二)图片下载,进度条

发布时间:2020-06-05 07:14:59 作者:just2012xia
来源:网络 阅读:330
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 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 ProgressDialog dialog;
    private Button button;
    private ImageView p_w_picpathView;
    private String p_w_picpath_path = "http://i1.juyouqu.com/uploads/content//2014/04/1396611698344.jpg!w598";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dialog = new ProgressDialog(this);
        dialog.setTitle("提示");
        dialog.setMessage("正在下载.......");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//      dialog.show();
        button = (Button)findViewById(R.id.button1);
        p_w_picpathView = (ImageView)findViewById(R.id.p_w_picpathView1);
        button.setOnClickListener(new View.OnClickListener() {
          
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new MyTask().execute(p_w_picpath_path);
            }
        });
    }
    public class MyTask extends AsyncTask<String, Integer, Bitmap>
    {
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            dialog.show();
        }
        @Override
        protected Bitmap doInBackground(String... params) {
            // TODO Auto-generated method stub
            Bitmap bitmap = null;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            InputStream input = null;
              
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(params[0]);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                      
                    input = httpResponse.getEntity().getContent();
                    long file_length = httpResponse.getEntity().getContentLength();
                    int len = 0;
                    byte[] data = new byte[1024];
                    int total_length = 0;
                    int value = 0;
                    while((len = input.read(data)) != -1)
                    {
                        total_length += len;
                        value = (int)((total_length / (float)file_length) * 100);
                        publishProgress(value);
                        output.write(data, 0, len);
                    }
                    byte[] result = output.toByteArray();
                    bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
                      
                }
                  
            } catch (Exception e) {
                // TODO: handle exception
            }finally
            {
                if(input != null)
                    try {
                        input.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                if(output != null)
                    try {
                        output.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
          
            dialog.dismiss();
            p_w_picpathView.setImageBitmap(result);
        }
        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            dialog.setProgress(values[0]);
        }
    }
}


推荐阅读:
  1. AsyncTask的用法
  2. android AsyncTask介绍

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

import 图片下载 cta

上一篇:Docker+k8s+微服务部署方案

下一篇:MongoDB 在win 2012系统安装报错

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》