package com.example.picdemo;
import java.io.InputStream;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ImageView imageView;
private ProgressBar pb;
String path = "http://192.168.137.189:8080/ab.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
pb = (ProgressBar) findViewById(R.id.pb);
}
public void getPic(View v) {
MyAsyncTask asyncTask = new MyAsyncTask();
// 执行一个异步任务
asyncTask.execute(path);
}
// 参数 如果没有参数 Void Params 代表路径
// progress 进度类型
// Result 子线程中请求的结果
class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... parUrl) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(parUrl[0]);
HttpResponse httpResponse = client.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
InputStream content = httpResponse.getEntity().getContent();
Bitmap decodeStream = BitmapFactory.decodeStream(content);
return decodeStream;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bitmap result) {
pb.setVisibility(View.GONE);
publishProgress(50);
imageView.setImageBitmap(result);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
// 设置进度
pb.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
}
import java.io.InputStream;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ImageView imageView;
private ProgressBar pb;
String path = "http://192.168.137.189:8080/ab.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
pb = (ProgressBar) findViewById(R.id.pb);
}
public void getPic(View v) {
MyAsyncTask asyncTask = new MyAsyncTask();
// 执行一个异步任务
asyncTask.execute(path);
}
// 参数 如果没有参数 Void Params 代表路径
// progress 进度类型
// Result 子线程中请求的结果
class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... parUrl) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(parUrl[0]);
HttpResponse httpResponse = client.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
InputStream content = httpResponse.getEntity().getContent();
Bitmap decodeStream = BitmapFactory.decodeStream(content);
return decodeStream;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bitmap result) {
pb.setVisibility(View.GONE);
publishProgress(50);
imageView.setImageBitmap(result);
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
// 设置进度
pb.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
super.onCancelled();
}
}
}