AsyncTask与HttpURLConnection下载网络图片
2016-08-18 20:25
399 查看
使用AsyncTask可以更加方便的在子线程中对UI进行操作。
AsyncTask是个抽象类,使用它必须要创建一个子类去继承它。他有三个泛型的参数,
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="yifang.zhsong.ansyctaskdemo.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download" android:id="@+id/bt_download" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="43dp" /> <ImageView android:layout_width="500dp" android:layout_height="500dp" android:id="@+id/iv_picture" /> </RelativeLayout>
MainActivity部分的代码 package yifang.zhsong.ansyctaskdemo; import android.app.ProgressDialog; 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.Button; import android.widget.ImageView; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { private Button button; private ImageView image; private ProgressDialog dia; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.bt_download); image = (ImageView) findViewById(R.id.iv_picture); //定义进度条 dia = new ProgressDialog(this); dia.setTitle("提示信息"); dia.setMessage("下载中,请稍后"); dia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置点击进度条外部,不响应; dia.setCancelable(false); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new MyTask().execute(); } }); } //Sting 表示传入的值, Integer 代表进度 , Bitmap 代表返回的值 public class MyTask extends AsyncTask<String, Integer, Bitmap> { //任务执行之前的准备工作。 @Override protected void onPreExecute() { // TODO Auto-generated method stub dia.show(); } //主要完成耗时操作 @Override protected Bitmap doInBackground(String... arg0) { // TODO Auto-generated method stub HttpURLConnection connection = null; Bitmap bitmap = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = null; try { URL url = new URL("http://imglf0.nosdn.127.net/img/akF3Q3JLbzVNWExja3g3d1B5a0NRcW50OUFtNTBoNTRaYkRjdEo2WlBDeGRTRkNGUUVraGxnPT0.jpg?imageView&thumbnail=500x0&quality=96&stripmeta=0&type=jpg"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); inputStream = connection.getInputStream(); //先获得文件的总长度, int fileLength = connection.getContentLength(); int len = 0; byte[] data = new byte[1024]; int total_length = 0; int value = 0; while ((len = inputStream.read(data)) != -1) { total_length += len; value = ((total_length / fileLength) * 100); //调用update函数,更新进度 publishProgress(value); outputStream.write(data, 0, len); Thread.sleep(500); } byte[] result = outputStream.toByteArray(); bitmap = BitmapFactory.decodeByteArray(result, 0, result.length); inputStream.close(); outputStream.close(); connection.disconnect(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); outputStream.close(); connection.disconnect(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return bitmap; } //更新进度条 @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub dia.setProgress(values[0]); } //主要完成耗时操作 @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub dia.dismiss(); image.setImageBitmap(result); } } }
相关文章推荐
- 0314-使用HttpURLConnection下载网络图片并显示
- 使用HttpURLConnection和AsyncTask从网络缓存图片
- 第四章 网络下载之httpurlconnection 从网络上下载一张图片,在本地显示
- 关于HttpUrlConnection加载和下载网络图片的学习
- 使用HttpURLConnection下载网络图片并显示
- Android HttpURLConnection获取网络下载图片POST请求方式
- HttpURLConnection下载网络图片
- Android - HttpURLConnection下载网络文件,加载网络图片
- 使用HttpURLConnection下载网络图片
- HttpURLConnection下载网络图片
- 使用HttpURLConnection从网络中下载图片
- android httpUrlConnection 方式从网络上面下载图片
- android HttpURLConnection编程详解,获取网络图片,html代码查
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Fragment中存放GridView,结合LruCache、DiskLruCache和HttpURLConnection实现图片下载、缓存
- HttpURLConnection根据URL下载图片
- HttpURLConnection获取网络图片
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- 【Android进阶】使用HttpURLConnection实现图片的下载与现显示
- android网络HttpURLConnection抓取网络图片