title: How to download a file with Retrofit2?
body: <p>How can I <em>download</em> a <strong>file(image/video)</strong> from my <strong>PHP</strong> server using <strong>Retrofit2</strong> ?</p>
  
  <p>I wasn't able to find any resources or tutorials online on how to proceed; I found <a href="https://stackoverflow.com/questions/32856850/retrofit-2-file-down-upload">this post</a> that treats a certain download error on <strong>SO</strong> but it's not very clear to me. Could anyone point me to the right direction? </p>
  
  <h2>UPDATE:</h2>
  
  <p>Here is my code:</p>
  
  <p><strong>FileDownloadService.java</strong></p>
  
  <pre><code>public interface FileDownloadService {
      @GET(Constants.UPLOADS_DIRECTORY + "/{filename}")
      @Streaming
      Call&lt;ResponseBody&gt; downloadRetrofit(@Path("filename") String fileName);
  }
  </code></pre>
  
  <p><strong>MainActivity.java</strong> (<strong>@Blackbelt</strong>'s solution)</p>
  
  <pre><code>private void downloadFile(String filename) {
      FileDownloadService service = ServiceGenerator
              .createService(FileDownloadService.class, Constants.SERVER_IP_ADDRESS);
      Call&lt;ResponseBody&gt; call = service.downloadRetrofit("db90408a4bb1ee65d3e09d261494a49f.jpg");
  
      call.enqueue(new Callback&lt;ResponseBody&gt;() {
          @Override
          public void onResponse(final Response&lt;ResponseBody&gt; response, Retrofit retrofit) {
              try {
                  InputStream is = response.body().byteStream();
                  FileOutputStream fos = new FileOutputStream(
                          new File(Environment.getExternalStorageDirectory(), "image.jpg")
                  );
                  int read = 0;
                  byte[] buffer = new byte[32768];
                  while ((read = is.read(buffer)) &gt; 0) {
                      fos.write(buffer, 0, read);
                  }
  
                  fos.close();
                  is.close();
              } catch (Exception e) {
                  Toast.makeText(MainActivity.this, "Exception: " + e.toString(), Toast.LENGTH_LONG).show();
              }
          }
  
          @Override
          public void onFailure(Throwable t) {
              Toast.makeText(MainActivity.this, "Failed to download file...", Toast.LENGTH_LONG).show();
          }
      });
  }
  </code></pre>
  
  <p>I get a <strong>FileNotFoundException</strong> when USB debugging is active, &amp; a <strong>NetworkOnMainThreadException</strong> when not. </p>
  
  <p><strong>MainActivity.java:</strong> (<strong>@Emanuel</strong>'s solution)</p>
  
  <pre><code>private void downloadFile(String filename) {
      FileDownloadService service = ServiceGenerator
              .createService(FileDownloadService.class, Constants.SERVER_IP_ADDRESS);
      Call&lt;ResponseBody&gt; call = service.downloadRetrofit("db90408a4bb1ee65d3e09d261494a49f.jpg");
  
      call.enqueue(new Callback&lt;ResponseBody&gt;() {
          @Override
          public void onResponse(final Response&lt;ResponseBody&gt; response, Retrofit retrofit) {
              Log.i(TAG, "external storage = " + (Environment.getExternalStorageState() == null));
              Toast.makeText(MainActivity.this, "Downloading file... " + Environment.getExternalStorageDirectory(), Toast.LENGTH_LONG).show();
  
              File file = new File(Environment.getDataDirectory().toString() + "/aouf/image.jpg");
              try {
                  file.createNewFile();
                  Files.asByteSink(file).write(response.body().bytes());
              } catch (Exception e) {
                  Toast.makeText(MainActivity.this,
                          "Exception: " + e.toString(),
                          Toast.LENGTH_LONG).show();
              }
          }
  
          @Override
          public void onFailure(Throwable t) {
              Toast.makeText(MainActivity.this, "Failed to download file...", Toast.LENGTH_LONG).show();
          }
      });
  }
  </code></pre>
  
  <p>I get a <strong>FileNotFoundException</strong>.</p>