1. main.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=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<pre name="code" class="java">import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
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 {
private Button myButton = null;
private ImageView imageView = null;
private String image_path = "http://11.203.18.14:8080/image/001.png";
private ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button)findViewById(R.id.button1);
imageView = (ImageView)findViewById(R.id.imageView1);
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("提示信息");
dialog.setMessage("正在下载,请稍后......");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //水平显示
dialog.setCancelable(false);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new MyTask().execute(image_path);
}
});
}
public class MyTask extends AsyncTask<String, Integer, 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 Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
//完成图片下载功能
Bitmap bitmap = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream inputStream = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
inputStream = httpResponse.getEntity().getContent();
long file_length = httpResponse.getEntity().getContentLength();
int len = 0;
byte[] data = new byte[1024];
int total_length = 0;
while((len=inputStream.read(data))!=-1){
total_length+=len;
int value = (int)((total_length/(float)file_length)*100);
publishProgress(value);
outputStream.write(data, 0, len);
outputStream.flush();
}
byte[] result = outputStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
}
} catch (Exception e) {
// TODO Auto-generated catch block
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);
dialog.dismiss();
imageView.setImageBitmap(result);
}
}
@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;
}
}3. android注册文件一定要加访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>
本文介绍了一个简单的Android应用程序,该程序通过按钮点击触发图片的异步下载,并使用进度对话框显示下载进度。下载完成后,图片将显示在ImageView中。
3064

被折叠的 条评论
为什么被折叠?



