標籤:android開發 download image imageview asynctask
今天工作需要寫了一個保留網狀圖片緩衝的類,和大家分享一下
其實實現原理很簡單,也就是從網上下載圖片資料,一邊將資料轉成drawable並載入到指定的imageview
一邊儲存成download_image.jpg,在點擊imageview時候用intent將圖片開啟
我將處理圖片的過程寫成了類
package com.example.downloadandopenimage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import android.content.Context;import android.content.Intent;import android.graphics.drawable.Drawable;import android.net.Uri;import android.os.AsyncTask;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.ProgressBar;public class LoadAndSaveImage {ImageView view;Context mContext;public LoadAndSaveImage() {// TODO Auto-generated constructor stub}public LoadAndSaveImage(Context context, String str, ImageView view) {// TODO Auto-generated constructor stubthis.view = view;mContext = context;new Load_and_save_image().execute(str);}void release() {File file = new File("file://"+ Environment.getExternalStorageDirectory().toString()+ "/download_image.jpg");if (file.exists()) {boolean deleted = file.delete();}}class Load_and_save_image extends AsyncTask<String, Void, Void> {@Overrideprotected Void doInBackground(String... sUrl) {// TODO Auto-generated method stubInputStream input = null;OutputStream output = null;HttpURLConnection connection = null;try {URL url = new URL(sUrl[0]);connection = (HttpURLConnection) url.openConnection();connection.connect();// download the fileinput = connection.getInputStream();File checker = new File("file://"+ Environment.getExternalStorageDirectory().toString()+ "/download_image.jpg");if (checker.exists()) {boolean deleted = checker.delete();}output = new FileOutputStream(Environment.getExternalStorageDirectory().toString()+ "/download_image.jpg");byte data[] = new byte[4096];int count;while ((count = input.read(data)) != -1) {output.write(data, 0, count);}} catch (Exception e) {} finally {try {if (input != null)input.close();if (output != null) {output.close();}} catch (IOException ignored) {}if (connection != null)connection.disconnect();}return null;}protected void onPostExecute(Void result) {Drawable d = Drawable.createFromPath(Environment.getExternalStorageDirectory().toString()+ "/download_image.jpg");view.setImageDrawable(d);view.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);intent.setDataAndType(Uri.parse("file://"+ Environment.getExternalStorageDirectory().toString() + "/download_image.jpg"),"image/*");mContext.startActivity(intent);}});};}}
只要在activity中這樣寫:
image = (ImageView) findViewById(R.id.image);new LoadAndSaveImage(getApplicationContext(), "http://toolongdidntread.com/wp-content/uploads/2011/06/upload.png", image);
三個參數分別是content,圖片的url,你要載入的imageview
BTW,由於這裡設定的儲存的jpg檔案名稱字是固定的,載入多張圖片時請用隨機數或者自行命名
源碼在這裡:→http://download.csdn.net/detail/edwardwayne/8621419
android載入網狀圖片並保留緩衝,隨時點擊開啟