关闭

AsyncTask 异步任务下载网络图片

121人阅读 评论(0) 收藏 举报
分类:
package com.qf.day07_asynctask_03;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private ImageView iv01,iv02;
	
	private String urlStr01 = "http://www.818dxs.com/uploads/allimg/160430/1-160430154611959.png";
	private String urlStr02 = "http://upload.cbg.cn/2015/1126/1448506973451.jpg";
	
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        iv01 =(ImageView) findViewById(R.id.iv01);
        iv02 = (ImageView) findViewById(R.id.iv02);
        
    }

    public void downLoad(View v)
    {
    	//启动异步任务, 下载图片
    	
    	new MyTask().execute(urlStr01,urlStr02);
    }
    
    public class MyTask extends AsyncTask<String, Void, List<Bitmap>>
    {
    	
    	private ProgressDialog pDialog;
    	
    	@Override
    	protected void onPreExecute() {
    		// TODO 初始化进度条对话框
    		super.onPreExecute();
    		
    		pDialog = new ProgressDialog(MainActivity.this);
    		
    		pDialog.setIcon(R.drawable.ic_launcher);
    		
    		pDialog.setTitle("提示");
    		
    		pDialog.setMessage("正在加载,请稍后...");
    		
    		pDialog.setCancelable(false);
    		
    		pDialog.show();
    	}
    	
    	
    	@Override
    	protected List<Bitmap> doInBackground(String... params) {
    		// TODO 执行耗时的操作
    		
    		List<Bitmap> data = new ArrayList<Bitmap>();
			
    		
    		try {
    			
    			for(int i=0;i<params.length;i++)
    			{
    				URL url = new URL(params[i]);
    				
    				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    				
    				conn.setRequestMethod("GET");
    				
    				conn.connect();
    				
    				if(conn.getResponseCode() == 200)
    				{
    					InputStream is = conn.getInputStream();
    					
    					Bitmap  bitmap = BitmapFactory.decodeStream(is);
    					
    					data.add(bitmap);
    					
    				}
    			}
    			
    			
				
			} catch (Exception e) {
				// TODO: handle exception
			}
    		
    		return data;
    	}
    	
    	@Override
    	protected void onPostExecute(List<Bitmap> result) {
    		// TODO 对话框消失, 更新UI
    		super.onPostExecute(result);
    		
    		pDialog.dismiss();
    		
    		if(result!=null && result.size()>0)
    		{
    			Log.i("info", "--"+result.size());
    			
    			iv01.setImageBitmap(result.get(0));
    			
    			iv02.setImageBitmap(result.get(1));
    		}
    		else
    		{
    			Toast.makeText(MainActivity.this, "加载失败", Toast.LENGTH_LONG).show();
    		}
    	}
    }
    
}




}
<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=".MainActivity" >

     <Button 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="下载图片"
         android:id="@+id/button"
         android:onClick="download"
         android:layout_centerHorizontal="true"
         />
     <ImageView 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/iv1"
         android:layout_below="@+id/button"
         android:layout_centerHorizontal="true"
         android:src="@drawable/ic_launcher"
         />
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv2"
    android:layout_below="@+id/iv1"
    android:layout_centerHorizontal="true"
    android:src="@drawable/ic_launcher"
    />
</RelativeLayout>

记得在清单文件中 加入访问网络的权限
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29555次
    • 积分:1617
    • 等级:
    • 排名:千里之外
    • 原创:133篇
    • 转载:25篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论