Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之異步任務加載網絡圖片並存儲在sdcard中(源代碼分享)

Android開發之異步任務加載網絡圖片並存儲在sdcard中(源代碼分享)

編輯:關於Android編程

在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"; //圖片所在的url
	private ImageView imageView;
	@Override
	protected 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() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new MyTask().execute(path);//啟動異步任務
			}
		});
	}

	public class MyTask extends AsyncTask {
		private String imageName;
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog.show();
		}

		@Override
		protected byte[] doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			byte []data=HttpUtiles.downloadImg(arg0[0]);
			//獲取圖片名稱
			imageName=arg0[0].substring(arg0[0].lastIndexOf("/")+1,arg0[0].length() );
			Log.i("TAG", "-------->"+data.length);
			return data;
		}

		@Override
		protected void onPostExecute(byte[] result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length);
			//顯示圖片
			imageView.setImageBitmap(bitmap);
			FileUtiles fileUtiles=new FileUtiles();
			fileUtiles.fileSave(imageName, result);
			dialog.dismiss();
		}
	}

	@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;
	}

}

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 block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				client.getConnectionManager().shutdown();
			}
		return data;
	}

存儲在sdcard中的代碼在上一篇博客中已貼出,在這篇博客中就不在寫了,Android通過AsyncTask加載網絡數據在實際開發中有著很大的作用,因耗時長的網絡操作放在主線程中會堵塞UI,就要采取多線程的方式解決該問題。最後一定不要忘了在 清單文件中添加

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved