package com.example.asynctask;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
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 org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.AsyncTaskLoader;
import android.content.Entity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button button;
ImageView img;
ProgressDialog prodialog;
private String imagpath="http://c.hiphotos.baidu.com/album/w%3D2048/sign=483d6850f3d3572c66e29bdcbe2b6127/8644ebf81a4c510f9b15dda36159252dd52aa5b1.jpg";
//private String imagpath="http://pic1.win4000.com/wallpaper/5/5034895771d66.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button1);
img=(ImageView) findViewById(R.id.imageView1);
prodialog=new ProgressDialog(this);
prodialog.setTitle("提示信息:");
prodialog.setMessage("正在下载,请稍等......");
prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
button.setOnClickListener( new View.OnClickListener(){
@Override
public void onClick(View v) {
new Mytask().execute(imagpath);
}
});
}
/*
*
*
* */
/*public class Mytask extends AsyncTask<String, Void, Bitmap>{
Bitmap bitmap;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
prodialog.show();
}
@Override
protected Bitmap doInBackground(String... params) {
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
try {
HttpResponse httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity entity=httpResponse.getEntity();
byte[] data=EntityUtils.toByteArray(entity);
bitmap=BitmapFactory.decodeByteArray(data, 0, data.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 onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prodialog.dismiss();
img.setImageBitmap(bitmap);
}
}*/
public class Mytask extends AsyncTask<String, Integer, Bitmap>{
Bitmap bitmap;
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
InputStream inputStream=null;
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
try {
HttpResponse httpResponse=httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
inputStream=httpResponse.getEntity().getContent();
long file_length=httpResponse.getEntity().getContentLength();
int len=0;
long total_length=0;
byte[] data=new byte[1024];
while((len=inputStream.read(data))!=-1){
System.out.println("a "+len+"---"+file_length);
total_length+=len;
System.out.println("total_length "+total_length);
outputStream.write(data, 0, len);
int values=(int) ((total_length/(float)file_length)*100);
publishProgress(values);
System.out.println("p "+values);
}byte[] result=outputStream.toByteArray();
bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inputStream!=null)
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
prodialog.dismiss();
img.setImageBitmap(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
prodialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
prodialog.setProgress(values[0]);
}
}
@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;
}
}
异步加载图片
最新推荐文章于 2021-03-02 09:03:57 发布