title: caching image and load them
body: <p>I downloaded images and cached them into my cache folder by using Fedora's lazylist but now, instead of displaying them into a listview. I need to make use of a viewpager + touchimageview. I have already found the example but have no idea how to load them into the AndroidTouchGallery. Can someone give me pointers?</p>
  
  <p>This is the filecache.java</p>
  
  <pre><code>package com.fedorvlasov.lazylist;
  
  import java.io.File;
  import java.net.URLEncoder;
  
  import android.content.Context;
  
  public class FileCache {
  
      private File cacheDir;
  
      public FileCache(Context context){
          //Find the dir to save cached images
          if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
              cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
          else
              cacheDir=context.getCacheDir();
          if(!cacheDir.exists())
              cacheDir.mkdirs();
      }
  
      public File getFile(String url){
          //I identify images by hashcode. Not a perfect solution, good for the demo.
          //String filename=String.valueOf(url.hashCode());
          //Another possible solution (thanks to grantland)
          String filename = URLEncoder.encode(url);
          File f = new File(cacheDir, filename);
          return f;
  
      }
  
      public void clear(){
          File[] files=cacheDir.listFiles();
          if(files==null)
              return;
          for(File f:files)
              f.delete();
      }
  
  }
  </code></pre>
  
  <p>the memory cache (also by fedora)</p>
  
  <pre><code>package com.fedorvlasov.lazylist;
  
  import java.util.Collections;
  import java.util.Iterator;
  import java.util.LinkedHashMap;
  import java.util.Map;
  import java.util.Map.Entry;
  import android.graphics.Bitmap;
  import android.util.Log;
  
  public class MemoryCache {
  
      private static final String TAG = "MemoryCache";
      private Map&lt;String, Bitmap&gt; cache=Collections.synchronizedMap(
              new LinkedHashMap&lt;String, Bitmap&gt;(10,1.5f,true));//Last argument true for LRU ordering
      private long size=0;//current allocated size
      private long limit=1000000;//max memory in bytes
  
      public MemoryCache(){
          //use 25% of available heap size
          setLimit(Runtime.getRuntime().maxMemory()/4);
      }
  
      public void setLimit(long new_limit){
          limit=new_limit;
          Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");
      }
  
      public Bitmap get(String id){
          try{
              if(!cache.containsKey(id))
                  return null;
              //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 
              return cache.get(id);
          }catch(NullPointerException ex){
              ex.printStackTrace();
              return null;
          }
      }
  
      public void put(String id, Bitmap bitmap){
          try{
              if(cache.containsKey(id))
                  size-=getSizeInBytes(cache.get(id));
              cache.put(id, bitmap);
              size+=getSizeInBytes(bitmap);
              checkSize();
          }catch(Throwable th){
              th.printStackTrace();
          }
      }
  
      private void checkSize() {
          Log.i(TAG, "cache size="+size+" length="+cache.size());
          if(size&gt;limit){
              Iterator&lt;Entry&lt;String, Bitmap&gt;&gt; iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated  
              while(iter.hasNext()){
                  Entry&lt;String, Bitmap&gt; entry=iter.next();
                  size-=getSizeInBytes(entry.getValue());
                  iter.remove();
                  if(size&lt;=limit)
                      break;
              }
              Log.i(TAG, "Clean cache. New size "+cache.size());
          }
      }
  
      public void clear() {
          try{
              //NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78 
              cache.clear();
              size=0;
          }catch(NullPointerException ex){
              ex.printStackTrace();
          }
      }
  
      long getSizeInBytes(Bitmap bitmap) {
          if(bitmap==null)
              return 0;
          return bitmap.getRowBytes() * bitmap.getHeight();
      }
  }
  </code></pre>
  
  <p>this is the part where i am supposed to load the cached data into the touchgallery. Previously I just needed to load in an array of strings (data was stored in the sdcard), now how do i display the items I downloaded as cache over at touchgallery?</p>
  
  <pre><code>public class UrlTouchImageView extends RelativeLayout {
      protected ProgressBar mProgressBar;
      protected TouchImageView mImageView;
  
      protected Context mContext;
      UrlPagerAdapter pagerAdapter;
      String[] mImageIds;
      Bitmap bm;
      ArrayList&lt;String&gt;  mStringList= new ArrayList&lt;String&gt;();
  
  
      public UrlTouchImageView(Context ctx, UrlPagerAdapter pagerAdapter)
      {
          super(ctx);
          mContext = ctx;
          this.pagerAdapter = pagerAdapter;
  
          init();
  
      }
      public UrlTouchImageView(Context ctx, AttributeSet attrs)
      {
          super(ctx, attrs);
          mContext = ctx;
          init();
      }
  
      public TouchImageView getImageView() 
      { 
          return mImageView; 
      }
  
  
      protected void init() {
  
          mImageView = new TouchImageView(mContext);
          LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
          mImageView.setLayoutParams(params);
          this.addView(mImageView);
          mImageView.setVisibility(GONE);
  
  
          mProgressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);
          params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
          params.addRule(RelativeLayout.CENTER_VERTICAL);
          params.setMargins(30, 0, 30, 0);
          mProgressBar.setLayoutParams(params);
          mProgressBar.setIndeterminate(true);
          this.addView(mProgressBar);
      }
  
      public void setUrl(String imageUrl)
      {
          new ImageLoadTask().execute(imageUrl);
      }
  
      //No caching load
      public class ImageLoadTask extends AsyncTask&lt;String, Integer, Bitmap&gt;
      {
          //RETRIEVES LINK FROM GALLERYACTIVITY
          //READ MNT/SDCARD/....
  
          @Override
          protected Bitmap doInBackground(String... strings) {
              bm = null;
              String url = strings[0];
              //Log.d("url",url);
  
              bm = BitmapFactory.decodeFile(url);
              return bm;
  
          }
  
          @Override
          protected void onPostExecute(Bitmap bitmap) {
  
              mImageView.setImageBitmap(bm);
              mImageView.setVisibility(VISIBLE);
              mProgressBar.setVisibility(GONE);
          }
      }
  }
  </code></pre>
  
  <p>I am sorry for my bad english as well as poor knowledge to android programming. hopefully you all can give me advices.. </p>