使用Asynctask的方法加载图片非常的慢
通过log我发现从new newAsynctask(image,url).execute(url);开始到加载图片需要很长时间,而thread方法则很快,不是网络的问题,是什么问题呢
package com.example.asynvtask_learn; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Handler; import android.os.Message; import android.support.v4.util.LruCache; import android.util.Log; import android.widget.ImageView; class Imageload{ private ImageView image; private String url; private LruCache<String, Bitmap> mcache;//用于缓存 private Handler mhandler = new Handler(){ public void handleMessage(Message msg) { if(image.getTag().equals(url)){ image.setImageBitmap((Bitmap) msg.obj );} }; }; public Imageload() { // TODO Auto-generated constructor stub //缓存 int maxMemory=(int) Runtime.getRuntime().maxMemory();//获取最大内存 int Memorysize=maxMemory/4; mcache=new LruCache<String, Bitmap>(Memorysize){ @Override protected int sizeOf(String key, Bitmap value) { // TODO Auto-generated method stub return value.getByteCount();//Bitmap的大小 } }; //缓存 } //加入缓存 public void addbitmaptocache(String url,Bitmap bitmap){ if(getbitmapfromcache(url)==null){ mcache.put(url, bitmap); } } //获取缓存 public Bitmap getbitmapfromcache(String url) { Bitmap mbitmap=mcache.get(url); return mbitmap; } public Bitmap getbitmap(String surl){ Bitmap bitmap = null; InputStream is = null; try { URL url=new URL(surl); HttpURLConnection htc=(HttpURLConnection) url.openConnection(); is=new BufferedInputStream(htc.getInputStream()); bitmap=BitmapFactory.decodeStream(is); htc.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return bitmap; } //thread方法 public void showimagebythread(ImageView image,final String url){ this.image=image; this.url=url; new Thread(){ @Override public void run() { // TODO Auto-generated method stub super.run(); Bitmap bt=getbitmap(url); Message message = Message.obtain(); message.obj=bt; mhandler.sendMessage(message); } }.start(); } //asynctask方法 public void showimagebyasynctask(ImageView image,String url){ Bitmap bitmap=getbitmapfromcache(url);//缓存中取出图片 Log.i("info", "取出了缓存"); if(bitmap==null){ //如果没有则要去下载 new newAsynctask(image,url).execute(url); Log.i("info", "下载"); }else{ image.setImageBitmap(bitmap); Log.i("info", "缓存加载"); } } class newAsynctask extends AsyncTask<String, Void, Bitmap>{ String url; ImageView imageView; public newAsynctask(ImageView imageView,String url) { // TODO Auto-generated constructor stub this.imageView=imageView; this.url=url; } @Override protected Bitmap doInBackground(String... params) { // TODO Auto-generated method stub String murl=params[0]; Bitmap bitmap=getbitmap(murl); if(bitmap!=null){ //加入缓存 addbitmaptocache(murl,bitmap); Log.i("info", "加入了缓存"); } return bitmap; // return getbitmap(params[0]); } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); if(imageView.getTag().equals(url)){ imageView.setImageBitmap(result); Log.i("info", "下载加载"); } } } }