AsyncTask加载图片

package com.example.lenovo.linkai20170815;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
private String url="http://pic39.nipic.com/20140226/18071023_154707834000_2.jpg";
    private LinearLayout lt;
    private TextView tv_progress;
    private ProgressBar pg_bar;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new MyAsync().execute(url);
    }

    private void initView() {
        tv_progress = (TextView) findViewById(R.id.tv_progress);
        lt = (LinearLayout) findViewById(R.id.lt);
        pg_bar = (ProgressBar) findViewById(R.id.pg_bar);
        iv = (ImageView) findViewById(R.id.iv);

    }
    class MyAsync extends AsyncTask<String,Integer,Bitmap>
    {

        @Override
        protected void onPreExecute() {
        lt.setVisibility(View.VISIBLE);
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            try {
                URL url1=new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
                int responseCode = connection.getResponseCode();
                if(responseCode==200)
                {
                    InputStream inputStream = connection.getInputStream();
                    int len=0;
                    int progress=0;
                    int current=0;
                    int toatlLength = connection.getContentLength();
                    byte[] arr=new byte[1];
                    ByteArrayOutputStream bos=new ByteArrayOutputStream();
                    while((len=inputStream.read(arr))!=-1)
                    {   current+=len;
                        progress = (int) ((float)current/toatlLength * 100);
                        publishProgress(progress);
                        bos.write(arr,0,len);
                    }
                    return  BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size());
                }


            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            iv.setImageBitmap(bitmap);
            lt.setVisibility(View.VISIBLE);


        }

        @Override
        protected void onProgressUpdate(Integer... values) {
          pg_bar.setProgress(values[0]);
            tv_progress.setText(values[0]+"%");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值