首先先声明访问网络权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.asktaskdownload" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
布局文件
<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.asktaskdownload.MainActivity" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="29dp" android:text="下载网络图片" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/ic_launcher" /> </RelativeLayout>
package com.example.asktaskdownload; import org.apache.http.HttpEntity; 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 org.apache.http.util.EntityUtils; 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.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imageView; private Button button; //图片的网络地址 private String image_path = "http://pic4.nipic.com/20090828/2366808_092035042961_2.jpg"; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView1); button = (Button) findViewById(R.id.button1); dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setMessage("正在下载,请稍后..."); button.setOnClickListener(new OnClickListener() { //Android3以后不允许在UI主线程访问网络 @Override public void onClick(View v) { // TODO Auto-generated method stub new MyTask().execute(image_path); } }); } /* * 第一个参数:表示要执行的任务的参数,一般传递一个路径 * 第二个参数:进度的刻度 * 第三个参数:任务执行的返回结果 */ public class MyTask extends AsyncTask<String, Void, Bitmap>{ //任务执行之前的操作 @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.show(); } //完成下载或耗时任务操作 @Override protected Bitmap doInBackground(String... params) { HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(params[0]); Bitmap bitmap = null; try { HttpResponse httpResponse = httpClient.execute(httpGet); //返回的状态码为200表示操作成功 if(httpResponse.getStatusLine().getStatusCode() == 200){ //获取实体类 HttpEntity httpEntity = httpResponse.getEntity(); //将实体类转换成字节数组 byte[] data = EntityUtils.toByteArray(httpEntity); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); } }catch (Exception e) { e.printStackTrace(); } return bitmap; } //主要更新UI操作 @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); imageView.setImageBitmap(result); dialog.dismiss(); } } }
主页面,点击下载按钮
最后下载完成的结果
时间: 2024-10-11 09:46:05