Android AsyncTask下载图片和ProgressBar进度条

本文介绍了一个基于Android平台使用AsyncTask实现图片下载并显示的示例应用。该应用包含一个按钮,点击后开始从指定URL下载图片,并通过进度条展示下载进度,最后将下载的图片显示出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MainActivity

package cn.bgs.asynck_bar;


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


public class MainActivity extends Activity implements OnClickListener {
private Button mBtn;
private ProgressBar mBar_hor,mBar;
private ImageView mImg;
private MyTask task;
private String str="http://p2.so.qhimg.com/sdr/531_768_/t01b3d46b8470dbd212.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBtn=(Button) findViewById(R.id.mBtn);
mBar=(ProgressBar) findViewById(R.id.mBar);
mBar_hor=(ProgressBar) findViewById(R.id.mBar_hor);
mImg=(ImageView) findViewById(R.id.mImg);
mBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
task=new MyTask(mBar_hor, mBar, mImg);
task.execute(str);
}
}


AsyncTask.class

package cn.bgs.asynck_bar;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;


public class MyTask extends AsyncTask<String, Integer, Bitmap>{
private ProgressBar mBar_hor,mBar;
private ImageView mImg;

public MyTask(ProgressBar mBar_hor, ProgressBar mBar, ImageView mImg) {
super();
this.mBar_hor = mBar_hor;
this.mBar = mBar;
this.mImg = mImg;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mBar_hor.setVisibility(View.VISIBLE);
mBar.setVisibility(View.VISIBLE);
mImg.setVisibility(View.INVISIBLE);
}
@Override
protected Bitmap doInBackground(String... params) {
try {
//建立网络连接
URL url=new URL(params[0]);
//打开网络连接
URLConnection connection = url.openConnection();
int totallen=connection.getContentLength();
publishProgress(1,totallen);
//设置图片存储路径
String filepath=Environment.getExternalStorageDirectory()+"/tupian.jpg";
//创建文件
File file=new File(filepath);
if (!file.exists()) {
file.createNewFile();
}
//获取流
InputStream is = connection.getInputStream();
//创建输入流缓冲区
BufferedInputStream Bin=new BufferedInputStream(is);
//创建输出流缓冲区
BufferedOutputStream Bout=new BufferedOutputStream(new FileOutputStream(file));
//创建数组,按照数组的方式读取
byte [] b=new byte[1024];
int len=0;
//读取文件
while ((len=Bin.read(b))!=-1) {
Bout.write(b, 0, len);
publishProgress(2,len);
}
//记得关闭流
Bout.flush();
Bout.close();
Bin.close();
Bitmap bit=BitmapFactory.decodeFile(filepath);
return bit;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (values[0]==1) {
mBar_hor.setMax(values[1]);
} else if(values[0]==2){
mBar_hor.setProgress(mBar_hor.getProgress()+values[1]);
}
}
@Override
protected void onPostExecute(Bitmap bit) {
super.onPostExecute(bit);
mBar.setVisibility(View.GONE);
mImg.setImageBitmap(bit);
mImg.setVisibility(View.VISIBLE);
}
}

activity_main

<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"
    tools:context=".MainActivity" 
    android:orientation="vertical"
    >
<Button 
   android:id="@+id/mBtn"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="点击下载图片"
   />
<ProgressBar 
   android:id="@+id/mBar_hor"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   style="?android:attr/progressBarStyleHorizontal"
   android:visibility="invisible"
   />
<RelativeLayout 
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   >
   <ImageView 
       android:id="@+id/mImg"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:src="@drawable/ic_launcher"
       />
   <ProgressBar 
       android:id="@+id/mBar"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:visibility="invisible"
       />
</RelativeLayout>
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值