标签: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 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); } }); }; } }
image = (ImageView) findViewById(R.id.image); new LoadAndSaveImage(getApplicationContext(), "http://toolongdidntread.com/wp-content/uploads/2011/06/upload.png", image);
BTW,由于这里设置的储存的jpg文件名字是固定的,加载多张图片时请用随机数或者自行命名
源码在这里:→http://download.csdn.net/detail/edwardwayne/8621419
标签:android开发 download image imageview asynctask
原文地址:http://blog.csdn.net/edwardwayne/article/details/45198887