title: save images in cache android
body: <p>My android project layout xml file has three button. which are shows, events &amp; home. when buttons click they download images from url &amp; save them in cache. So now downloaded are completed once it is clicked. when click the shows button again it must show the downloaded images taking from cache. </p>
  
  <p>The problem is I download all the images in one cache dir. when button click separately they load default images. because can't identify images. my code as following way.</p>
  
  <pre><code>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(),"TNLRadio/res/shows");
              cacheDirEvent=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/events");
              cacheDirPromotion=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/promotion");
          }
          else{
              cacheDir=context.getCacheDir();
              cacheDirEvent=context.getCacheDir();
              cacheDirPromotion=context.getCacheDir();
          }
          if(!cacheDir.exists()&amp;&amp; !cacheDirEvent.exists() &amp;&amp; !cacheDirPromotion.exists()){
              cacheDir.mkdirs();
              cacheDirEvent.mkdir();
              cacheDirPromotion.mkdir();
          }
      }
  
      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;
  
      } 
  </code></pre>
  
  <p>pls help me. i'm stuck with this question</p>
  
  <p>After that I have done this. but same answer.......why is it?</p>
  
  <pre><code>public FileCache(Context context,int i){
          //Find the dir to save cached images
          if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)&amp;&amp; i==3)
              cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/shows");
          else if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)&amp;&amp; i==2)
              cacheDirEvent=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/events");
          else if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED) &amp;&amp; i==1)
              cacheDirHome=new File(android.os.Environment.getExternalStorageDirectory(),"TNLRadio/res/home");
          else 
              cacheDir=context.getCacheDir();
              cacheDirEvent=context.getCacheDir();
              cacheDirHome=context.getCacheDir();
          if(!cacheDir.exists())
              cacheDir.mkdirs();
          else if (!cacheDirHome.exists())
              cacheDirHome.mkdirs();
          else if  (!cacheDirEvent.exists())
              cacheDirEvent.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);
          if(url.substring(0, 26).equals("http://tnlradio/promotions")){
          File f = new File(cacheDirHome, filename);
          return f;}
          else if(url.equals("http://stream.tnlradio.com/images/dilshan-ishara.jpg")){
              File f = new File(cacheDirEvent, filename);
              return f;}
          else{
              File f = new File(cacheDir, filename);
              return f;
          }
  
      }
  </code></pre>
  
  <p>pls help me</p>