今天工作需要寫了一個保留網絡圖片緩存的類,和大家分享一下
其實實現原理很簡單,也就是從網上下載圖片數據,一邊將數據轉成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 stub
this.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> {
@Override
protected Void doInBackground(String... sUrl) {
// TODO Auto-generated method stub
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// download the file
input = 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() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent 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