Android学习篇章45-HttpClient网络下载-IO流传输-下载图片资源

Mainactivity:

public class MainActivity extends Activity {

	ImageView img1=null;
	ProgressDialog dialog=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		img1=(ImageView)findViewById(R.id.img1);
		dialog=new ProgressDialog(this);
		dialog.setTitle("下载图片");
		dialog.setMessage("正在下载请稍候....");
		dialog.setCancelable(false);
		dialog.setMax(100);
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	}
	public void clickBtn(View view)
	{
	      	new MyDownloadImgTask().execute("http://192.168.1.151/advmgr/test.jpg");
	}

	private class MyDownloadImgTask  extends AsyncTask<String, Integer, Bitmap>
	{

		@Override
		protected Bitmap doInBackground(String... params) {
			// 使用Httpclient发起get请求
			String path=params[0];
			HttpClient  client= new DefaultHttpClient();
			//设定get请求
			HttpGet  get=new HttpGet(path);
			try {
				//执行这个get请求
			 HttpResponse response=client.execute(get);
			//获得响应的entity
		  	  HttpEntity entity=response.getEntity();
		  	  //获得传回的图片数据的长度
		  	  long size=entity.getContentLength();
		  	  Log.i("test", "size="+size);
		  	  InputStream is=entity.getContent();
		  	  ByteArrayOutputStream  bos=new ByteArrayOutputStream();
		  	  byte[] buf=new byte[1024];
		  	  int length=0;
		  	  int sum=0;//用来记录已经读了多少字节的数据
		  	  while((length=is.read(buf))>0)
		  	  {
		  		  //写入到字节输出流中
		  		  bos.write(buf, 0, length);
		  		  SystemClock.sleep(10);
		  		  sum+=length;//记录总的读取的字节数
		  		  Log.i("test", "sum="+sum);
		  		  int progress=(int)(sum*100.0/size);
		  		  Log.i("test", "progress="+progress);
		  		  //更新进度
		  		  publishProgress(progress);
		  		  
		  	  }
		  	 return BitmapFactory.decodeByteArray(bos.toByteArray(), 0, (int)size);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			img1.setImageBitmap(result);
			//下载完毕后关闭进度对话框
			dialog.dismiss();
			super.onPostExecute(result);
		}

		//在下载任务开始之前显示进度对话框
		//当调用了publishProgress方法时就会更新
		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			dialog.setProgress(values[0]);
			super.onProgressUpdate(values);
		}
		@Override
		protected void onPreExecute() {
            dialog.show();
			super.onPreExecute();
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

xml:

<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"
    tools:context=".MainActivity" >

    <Button android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="clickBtn"
        android:text="图片下载"
        />
    <ImageView android:id="@+id/img1"
        android:layout_below="@+id/btn1"
        android:scaleType="fitCenter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />

</RelativeLayout>


权限:    <uses-permission android:name="android.permission.INTERNET"/>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值