android异步下载图片并显示水平进度条

activity_main.xml

<LinearLayout 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:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
	
    <Button 
  	    android:id="@+id/MyImageDownBtn"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    android:text="下载图片"
      />
  	<ImageView 
  	    android:id="@+id/myImageView"
  	    android:layout_width="wrap_content"
  	    android:layout_height="wrap_content"
  	    />
  

</LinearLayout>

MainActivity.java

package com.exmple.myimagedown;

import com.exmple.day6myimagedown.R;
import com.exmple.myimagedown.util.MyAsyncTask;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private MyAsyncTask myAsyncTask;
	private String urlString;
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button = (Button) findViewById(R.id.MyImageDownBtn);	
		urlString = "http://e.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=9f809759560fd9f9a0425d6d101df81c/f703738da9773912c7633d44ff198618367ae24a.jpg";
		imageView = (ImageView) findViewById(R.id.myImageView);
		
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				myAsyncTask = new MyAsyncTask(MainActivity.this,imageView);
				myAsyncTask.execute(urlString);
			}
		});
	}
}
MyAsyncTask.java
package com.exmple.myimagedown.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.R;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
	private int currentProgess;
	private int currentSize=0;
	private Bitmap bitmap;
	private Context context;
	private ImageView imageView;
	private ProgressDialog dialog;
	public MyAsyncTask(Context context, ImageView imageView) {
		// TODO Auto-generated constructor stub
		this.context=context;
		this.imageView=imageView;
	}
	@Override
	protected void onPreExecute() {
		Log.e("MyAsyncTask", "onPreExecute");
		dialog = new ProgressDialog(context);
		dialog.setTitle("My DownLoad");
		dialog.setMessage("Loading");
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setMax(100);
		dialog.show();
	}
	@Override
	protected Bitmap doInBackground(String... params) {
		// TODO Auto-generated method stub
		String urlString =params[0];
		try {
			URL url =new URL(urlString);
			HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
			httpURLConnection.setRequestProperty("Accept-Encoding", "identity");
			int totalSize=httpURLConnection.getContentLength();//总的大小
			Log.i("a", totalSize+"");
			InputStream is= httpURLConnection.getInputStream();
			byte [] buf=new byte[7];
			int len=0;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while(true){
				 len=is.read(buf, 0, buf.length);
				if(len<0){
					break;
				}
				Log.i("len", ""+len);
				currentSize = currentSize+len;
				Log.i("currentSize", ""+currentSize);
				currentProgess =(int) ((100.00*currentSize/totalSize));//这里如果是Current/totalSize的结果是0.注意这里的值。
				Log.i("currentProgess", ""+currentProgess);
				baos.write(buf, 0, len);
				publishProgress(currentProgess);
			}
			byte[] im=baos.toByteArray();
			bitmap = BitmapFactory.decodeByteArray(im, 0, im.length);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.e("MyAsyncTask", "doInBackground");
		return bitmap;
	}
	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
//		onProgressUpdate(currentProgess);
		Log.e("MyAsyncTask", "onProgressUpdate");
		super.onProgressUpdate(values);
		dialog.setProgress(values[0]);//进度一般可以用setProgerss来显示
	}
	@Override
	protected void onPostExecute(Bitmap result) {
		// TODO Auto-generated method stub
		Log.e("MyAsyncTask", "onPostExecute");
		dialog.dismiss();
		imageView.setImageBitmap(bitmap);//画图
		super.onPostExecute(result);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值