在MainActivity中的代碼
package com.example.f02_sdcard02;import android.os.AsyncTask;import android.os.Bundle;import android.app.Activity;import android.app.ProgressDialog;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {private Button button;private ProgressDialog dialog; //建立一個對話方塊private String path = "http://111.0.26.119:8080/http/hangzhou.jpg"; //圖片所在的urlprivate ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);dialog = new ProgressDialog(MainActivity.this);dialog.setTitle("提示");dialog.setMessage("load......");button = (Button) this.findViewById(R.id.button1);imageView=(ImageView)this.findViewById(R.id.imageView1);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubnew MyTask().execute(path);//啟動非同步任務}});}public class MyTask extends AsyncTask {private String imageName;@Overrideprotected void onPreExecute() {// TODO Auto-generated method stubsuper.onPreExecute();dialog.show();}@Overrideprotected byte[] doInBackground(String... arg0) {// TODO Auto-generated method stubbyte []data=HttpUtiles.downloadImg(arg0[0]);//擷取圖片名稱imageName=arg0[0].substring(arg0[0].lastIndexOf("/")+1,arg0[0].length() );Log.i("TAG", "-------->"+data.length);return data;}@Overrideprotected void onPostExecute(byte[] result) {// TODO Auto-generated method stubsuper.onPostExecute(result);Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);//顯示圖片imageView.setImageBitmap(bitmap);FileUtiles fileUtiles=new FileUtiles();fileUtiles.fileSave(imageName, result);dialog.dismiss();}}@Overridepublic 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;}}
HttpUtile擷取網路資料的方法
package com.example.f02_sdcard02;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import android.util.Log;public class HttpUtiles {public static byte[] downloadImg(String path) {byte[] data = null; HttpClient client=new DefaultHttpClient(); HttpPost httpPost=new HttpPost(path); try {HttpResponse httpResponse=client.execute(httpPost);if(httpResponse.getStatusLine().getStatusCode()==200){data=EntityUtils.toByteArray(httpResponse.getEntity());Log.i("TAG", "-------->"+data.length);}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{client.getConnectionManager().shutdown();}return data;}
儲存在sdcard中的代碼在上一篇部落格中已貼出,在這篇部落格中就不在寫了,Android通過AsyncTask載入網路資料在實際開發中有著很大的作用,因耗時間長度的網路操作放在主線程中會堵塞UI,就要採取多線程的方式解決該問題。最後一定不要忘了在 資訊清單檔中添加