天天看點

Android AsyncTask下載下傳圖檔和ProgressBar進度條

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>

繼續閱讀