title: Use OkHttp to download lots of images to sd card
body: <p>So a part of my app downloads a large number (~1600) of images to SdCard using an IntentService. I previously used Glide, but now want to switch to OkHttp, since it seems to be faster and less battery consuming. This is my current code:</p>
  
  <pre><code>for (int i = 1; i &lt; 1600; i++) {
          try {
              Request request = new Request.Builder()
                      .url(imageUrls[i])
                      .build();
  
              Response response = client.newCall(request).execute();
              File testDirectory = new File(Environment.getExternalStorageDirectory() 
                                                                    + "/downloadTest");
              if (!testDirectory.exists())
                   testDirectory.mkdirs();
              OutputStream outputStream = new FileOutputStream(new File(testDirectory, 
                                                            "testImage" + i + ".png"));
              InputStream inputStream = response.body().byteStream();
              byte[] buffer = new byte[1024];
              int read;
              while ((read = inputStream.read(buffer, 0, buffer.length)) &gt;= 0)
                  outputStream.write(buffer, 0, read);
              outputStream.flush();
              outputStream.close();
              inputStream.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Is there any way I could optimize this code to make downloading faster? </p>