title: Android Picasso - Save image locally before viewing it in ImageView
body: <p>I have made an app which shows a list of image. But before, I downloaded all the images to the sdcard, and I show a progress dialog with the number of downloaded files. I'm thinking to optimise the process using Picasso because of many images to download. So no progress dialog. With Picasso, I would like to save the image in my app directory (as a normal downloaded file) before it is shown in the ImageView. I thought to use this code :</p>
  
  <pre><code>Picasso picassoInstance = new Picasso.Builder(context)
   .downloader(new CustomDownloader(context, destination))
   .build();
  </code></pre>
  
  <p>The CustomDownloader :</p>
  
  <pre><code>public class CustomDownloader extends OkHttpDownloader {
  
      private File destination;
  
      public CustomDownloader(Context context, File destination){
          super(context);
          this.destination = destination;
      }
  
      @Override
      public Response load(Uri uri, boolean localCacheOnly) throws IOException {
          Response response = super.load(uri, networkPolicy);
  
          FileOutputStream fileOutput = new FileOutputStream(destination);
          InputStream inputStream = response.getInputStream();
  
          byte[] buffer = new byte[1024];
          int bufferLength;
          while ((bufferLength = inputStream.read(buffer)) &gt; 0) {
              fileOutput.write(buffer, 0, bufferLength);
          }
          fileOutput.close();
  
          return response;
      }
  }
  </code></pre>
  
  <p>And finally, I use this statement to load the image in the ImageView :</p>
  
  <pre><code>picassoInstance.load(item.getItem().getPath()).resize(width,0).into(imageView);
  </code></pre>
  
  <p>Am I correct to use this kind of implementation ? Or do you can provide another way ?</p>
  
  <p>EDIT 1 : I use the code above but the image is not shown in the ImageView, even if I see the images downloaded in the SDCARD.</p>
  
  <p>EDIT 2 : With this code, using new Thread :</p>
  
  <pre><code>@Override
  public Response load(Uri uri, boolean localCacheOnly) throws IOException {
      Response response = super.load(uri, networkPolicy);
      new Thread(new Runnable() {
          try {
              FileOutputStream fileOutput = new FileOutputStream(destination);
              InputStream inputStream = response.getInputStream();
  
              byte[] buffer = new byte[1024];
              int bufferLength;
              while ((bufferLength = inputStream.read(buffer)) &gt; 0) {
                  fileOutput.write(buffer, 0, bufferLength);
              }
              fileOutput.close();
          } catch(IOException e){
              e.printStackTrace();
          }
      }).start();
      return response;
  }
  </code></pre>
  
  <p>I got an <code>java.lang.ArrayIndexOutOfBoundsException: src.length=2048 srcPos=2048 dst.length=1024 dstPos=0 length=1024</code> in this line <code>while ((bufferLength = inputStream.read(buffer)) &gt; 0)</code></p>