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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asynctask_downloadpicture2.MainActivity$PlaceholderFragment" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="下載網絡圖片" />
</RelativeLayout>
一個activity:
package com.example.asynctask_downloadpicture2;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private Button button;
private ImageView imageView;
private ProgressDialog dialog;
private final String PIC_PATH="http://www.bz55.com/uploads/allimg/150309/139-150309101F7.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
button=(Button) findViewById(R.id.button1);
imageView=(ImageView) findViewById(R.id.imageView1);
dialog=new ProgressDialog(MainActivity.this);
dialog.setTitle("提示");
dialog.setMessage("下載圖片中,請稍後....");
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyAsyncTask().execute(PIC_PATH);
}
});
}
public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bitmap=null;
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
InputStream inputStream=null;
try {
HttpResponse respone=httpClient.execute(httpGet);
inputStream=respone.getEntity().getContent();
long pic_length=respone.getEntity().getContentLength();
byte[] data=new byte[1024];
int len=0;
int totoal_length=0;
while((len=inputStream.read(data))!=-1){
totoal_length+=len;
publishProgress((int) ((totoal_length / (float) pic_length) * 100));
outputStream.write(data, 0, len);
}
byte[] bitmapByte=outputStream.toByteArray();
bitmap=BitmapFactory.decodeByteArray(bitmapByte, 0, bitmapByte.length);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
imageView.setImageBitmap(result);
}
}
}