您的位置:首页 > 移动开发 > Android开发

Android AsyncTask下载图片和ProgressBar进度条

2017-09-11 15:53 344 查看
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.setVisib
a77f
ility(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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息