前不久搞的Android图片缓存,刚开始引入开源的框架,用着还行,但是在开发中遇到问题,就比如universal-image-loader-1.9.5.jar这个框架吧,在加载图片的时候自定义imageview无法加载,可能是存在以下问题吧,况且导入框架导致开发的项目包越来越大,基于上面的这几种情况,于是我就想自己写一个图片三级缓存的工具。
简要分析:刚开始想,图片的加载显示无非是先检查内存里面有没有,没就去文件里面找,若是文件里面没有的话就去开启网络下载,这样也符合开发中的大部分需求,而且效率肯定要高一点,在某些场合如快速滑动listview时从文件中加载图片就没有从内存中加载得快。先来看看效果吧!
这是第一次加载出来的图片,当然是网上的一张图片:
图片加载出来了,我们要看看日志,日志不会说谎,我截图的日志
刚才是本地和内存都没有那就的去网络下载了,所有日志打印的是开启网络下载
然而第二次进入应用的时候呢?还回去开启网络下载吗?那肯定不会的,要不这样要它有啥用呢?
看看吧这就是第二次进入应用的时候的日志
从文件中加载图片,不在需要开启网络下载直接从文件中获取,好了,这样问题就来了,从文件中读取肯定效率不好呀,没有直接从内存中读取快吧!我们调用系统维护好的一个算法,最近最少使用算法,这个算法是在我学操作系统的时候学到的,当时老师讲了一下,不过还好,能在项目中用到老师讲的东西,对此,以为就这个就完了?我们应该把文件中的图片读取到内存中,这样防止像listview这样的快速滑动产生的错位bug。下面看看我在快速滑动时的效果:
加载的图片都是从内存中获取,这样在效果上面就很好的啦!
好的,这么好的图片三级缓存,那到底怎么实现的呢以及到底怎么用的呢?下面就贴出我写的代码吧!让小伙伴看看,顺便优化优化!
package com.example.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.support.v4.util.LruCache;import android.util.Log;/** * 图片的三级缓存工具类{日后项目需要} * @author double 江 * */public class ImageCachceUitl { public static final int SUCCSEE = 0; public static final int FAIL = 1; private Context context; private LruCache<String, Bitmap> cache;//lru算法集合,string是图片的url,bitmap为图片的值类型 private File cacheDir; Handler handler; private ExecutorService executorService;//维护线性池 public ImageCachceUitl(Context context ,Handler handler){ this.context=context; this.handler=handler; cacheDir=context.getCacheDir();//获得cache文件夹 //维护几个网络线程下载图片 executorService=Executors.newFixedThreadPool(5); int maxSize=(int) (Runtime.getRuntime().maxMemory()/8);//获得运行环境的内存大小的1/8 cache=new LruCache<String, Bitmap>(maxSize){ // TODO 每存储一张图片的大小(作用于内存溢出丢图片) @Override protected int sizeOf(String key, Bitmap value) { //返回当前一行所占的字节数*高度,就是图片的大小 return value.getRowBytes()*value.getHeight(); } };//当前图片缓存总数的大小 } /** * * @param url 下载图片的连接 * @param position 需要显示图片的imgeView的Tag * @return */ public Bitmap getBitmapFromUrl(String url,int position){ //1内存中获取图片LRU算法 Bitmap bitmap=cache.get(url); //内存中有指定图片 if (bitmap!=null) { Log.i("从内存中获得图片", "从内存中获得图片"+url); return bitmap; } //2文件中获取图片 bitmap=getBitmapFromFile(url); if (bitmap!=null) { Log.i("从文件中获得图片", "从文件中获得图片"+url); return bitmap; } //3开启网络下载 Log.i("从网络中获得图片", "从网络中获得图片"+url); getBitmapFromNet(url,position); return null; } /** * 网络获取图片 * @param url 图片的;链接地址 * @param position 需要显示的imageview的tag */ private void getBitmapFromNet(String url, int position) { //开启任务 executorService.execute(new RunnableTask(url,position)); } //任务接口 class RunnableTask implements Runnable{ String imageUrl; int position; private HttpURLConnection httpURLConnection; public RunnableTask(String imageUrl, int position) { this.position=position; this.imageUrl=imageUrl; } @Override public void run() { //子线程的操作,开启网络下载图片 try { URL url=new URL(imageUrl); //请求对象 httpURLConnection=(HttpURLConnection) url.openConnection(); //网络请求的方式 httpURLConnection.setRequestMethod("GET"); //超时的时间, httpURLConnection.setConnectTimeout(5000); //读取超时的时间 httpURLConnection.setReadTimeout(5000); //httpURLConnection.getResponseCode()拿到最新数据 if (httpURLConnection.getResponseCode()==200) { //success get data from net;get tape InputStream inputStream=httpURLConnection.getInputStream(); //将流转化成bitmap图片 Bitmap bitmap=BitmapFactory.decodeStream(inputStream); //利用handler机制放入主线程中显示 Message msg=new Message(); //需要在主线程中显示的图片msg.obj msg.obj=bitmap; msg.arg1=position; //为msg设置标记 msg.what=SUCCSEE; handler.sendMessage(msg); //一,将下载完后的图片保存到内存中 cache.put(imageUrl, bitmap); //二,将下载完后的图片保存到文件中 writeToLoce(imageUrl,bitmap); return; } } catch (Exception e) { e.printStackTrace(); } //关闭请求 finally{ //断开服务器 if (httpURLConnection!=null) { httpURLConnection.disconnect(); } } //发送一个空消息 handler.obtainMessage(FAIL).sendToTarget(); } } /** * 图片写入cache文件夹下面的操作 * @param imageUrl * @param bitmap */ private void writeToLoce(String imageUrl, Bitmap bitmap) { try { String bitmapefilename=MD5Encoder.encode(imageUrl).substring(10); Log.i("bitmapefilename", bitmapefilename); File file=new File(cacheDir, bitmapefilename); FileOutputStream fileOutputStream =new FileOutputStream(file); //写入文件的操作(1图片类型2图片质量当为100时表示不压缩3文件流) bitmap.compress(CompressFormat.JPEG, 100, fileOutputStream); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 读取文件中图片的操作 * @param url 图片的连接地址 * @return */ private Bitmap getBitmapFromFile(String url) { try { //使用Md5工具加密截取前10个字符串 String bitmapefilename=MD5Encoder.encode(url).substring(10); /** * 在cache文件夹下面找到指定文件 * dir cache文件存储路径 * name 文件名称 */ File file=new File(cacheDir, bitmapefilename); // file.mkdir(); //FileInputStream fileInputStream=new FileInputStream(file); Bitmap bitmap=BitmapFactory.decodeFile(file.getPath());//完整文件路径 //Log.i("文件路径", file.getPath().toString()); //2读取之后放入内存,提高效率 cache.put(url, bitmap); return bitmap; } catch (Exception e) { e.printStackTrace(); } return null; }}
下面就是一个使用demo了,其实很简单
package com.example.do0me;
import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.security.PublicKey;import java.util.ArrayList;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.MarshalBase64;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.os.Handler;import android.util.Base64;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.ViewTreeObserver.OnPreDrawListener;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ImageView;import android.widget.ListView;import android.widget.Toast;import com.example.util.AgbcApi;import com.example.util.ClippingPicture;import com.example.util.FastBlur;import com.example.util.ImageCachceUitl;import com.tencent.connect.avatar.c;/** * 图片三级缓存的测试+图片上传 * @author double 江 * */public class MainActivity4 extends Activity { private ImageCachceUitl imageCachceUitl; private List<String> urlList=new ArrayList<String>(); private Runnable runnable; private Handler handler =new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case ImageCachceUitl.SUCCSEE: Bitmap bitmap=(Bitmap) msg.obj; int psition=msg.arg1; //通过TAg加载当前的limageview ImageView imageView=(ImageView) listview.findViewWithTag(psition); if (null!=bitmap&&null!=imageView) { imageView.setImageBitmap(bitmap); } break; case ImageCachceUitl.FAIL: Toast.makeText(getApplicationContext(), "下载错误", Toast.LENGTH_LONG).show(); default: break; } }; }; private ListView listview; private SoapObject request; private ExecutorService executorService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); listview=(ListView) findViewById(R.id.imageviewlist); imageCachceUitl=new ImageCachceUitl(getApplicationContext(), handler); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg");urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); listview.setAdapter(new myListAdapt()); executorService=Executors.newFixedThreadPool(5); executorService.execute(new Runnable() { @Override public void run() { getImageromSdk(); } }); } public String getImageFromAndroid(String arg0,String arg1, String arg2){ Log.i("进入端口方法", "进入端口方法"); final String methodName="getImageFromAndroid"; final String soapAction=AgbcApi.NAMESPACE+methodName; request = new SoapObject(AgbcApi.NAMESPACE, methodName); request.addProperty("arg0",arg1); request.addProperty("arg1",arg2); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); (new MarshalBase64()).register(envelope); envelope.bodyOut = request; envelope.dotNet=false; envelope.setOutputSoapObject(request); HttpTransportSE ht = new HttpTransportSE(AgbcApi.TASKSERVICEURL); ht.debug=true; try { ht.call(soapAction, envelope); Log.i("请求", envelope.bodyIn.toString()); } catch (IOException | XmlPullParserException e) { e.printStackTrace(); } return arg1; }; @SuppressLint("SdCardPath") public void getImageromSdk(){ Log.i("进入获取图片方法", "进入获取图片方法"); try{ String srcUrl = "/sdcard/"; //路径 String fileName = "1.png"; //文件名 String filrName2="2.jpg"; List<String>imageList=new ArrayList<>(); imageList.add(fileName); imageList.add(filrName2); for (int i = 0; i < imageList.size(); i++) { FileInputStream fis = new FileInputStream(srcUrl + imageList.get(i)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int count = 0; while((count = fis.read(buffer)) >= 0){ baos.write(buffer, 0, count); } String uploadBuffer = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT)); //进行Base64编码 String methodName = "uploadImage"; getImageFromAndroid(methodName,imageList.get(i), uploadBuffer); //调用webservice Log.i("connectWebService", "start"); fis.close();} }catch(Exception e){ e.printStackTrace(); } } class myListAdapt extends BaseAdapter{ private LayoutInflater layoutInflater; ImageView list_imag; Button list_but; @Override public int getCount() { // TODO Auto-generated method stub return urlList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return urlList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @SuppressLint({ "InflateParams", "ViewHolder" }) @Override public View getView(final int position, View convertView, ViewGroup parent) { layoutInflater=LayoutInflater.from(getApplication()); //if (layoutInflater==null) { convertView = layoutInflater.inflate(R.layout.image_list_item, null); // } list_but=(Button) convertView.findViewById(R.id.list_but); list_imag=(ImageView) convertView.findViewById(R.id.list_imag); list_imag.setTag(position); final Bitmap bitmap=imageCachceUitl.getBitmapFromUrl(urlList.get(position), position); if (null!=bitmap) { list_imag.setImageBitmap(bitmap); } list_imag.setVisibility(View.VISIBLE); list_but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //urlList.get(position); Log.i("获取点击焦点", "获取点击焦点"); // getImageromSdk(); //startRun(); new Thread(new Runnable() { @Override public void run() { getImageromSdk(); } }).start(); } }); return convertView; } }}
就为大家介绍到这吧,大家慢慢消化,希望文章对大家有或多或少的帮助。
新闻热点
疑难解答