title: NetworkOnmainThreadException android+Rxjava
body: <p>I have code that does write byte stream into a file and return the file
  but I am getting networkonmainthreadexception on writing input stream to file</p>
  <pre><code>accessTokenValidationWithResponseBodyForPDF(LinkApi.downloadFile(fileUrl))
                  .flatMap(new Function&lt;ResponseBody, ObservableSource&lt;File&gt;&gt;() {
                      @Override
                      public ObservableSource&lt;File&gt; apply(@io.reactivex.annotations.NonNull ResponseBody responseBody) throws Exception {
                          return Observable.just(writeResponseBodyToDisk(responseBody))
                                  .onErrorReturn(new Function&lt;Throwable, File&gt;() {
                                      @Override
                                      public File apply(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                                          return new File(AppConstants.not_valid);
                                      }
                                  });
                      }
                  })
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new DisposableObserver&lt;File&gt;() {
                      @Override
                      public void onNext(@NotNull File resultObject) {
                          try {
                              hideProgress();
                              if (resultObject.getPath().equals(AppConstants.not_valid)) {
  
                                  showAlertDialog(getString(R.string.something_went_wrong), true);
  
                              } else {
  
                                  File pdfFile = (File) resultObject;
                                  downloadedFile = pdfFile;
                                  if (pdfFile != null) {
                                      displayPdf(pdfFile);
                                  }
  
                              }
                          } catch (Exception e) {
                          }
  
                      }
  
                      @Override
                      public void onError(Throwable throwable) {
                          hideProgress();
                          
                      }
  
                      @Override
                      public void onComplete() {
  
                      }
                  });
  </code></pre>
  <pre><code>     private File writeResponseBodyToDisk(ResponseBody body) {
  
          
          String extStorageDirectory = getApplicationContext().getFilesDir().toString();
  
          File folder = new File(extStorageDirectory, &quot;.Link&quot;);
          if (!folder.exists())
              folder.mkdir();
  
          SecureRandom secureRandom = new SecureRandom();
          int random = secureRandom.nextInt();
  
          File futureStudioIconFile = new File(extStorageDirectory + File.separator + &quot;.Link&quot; + File.separator + &quot;link&quot; + random + &quot;.pdf&quot;);
  
          InputStream inputStream = null;
          OutputStream outputStream = null;
  
          try {
              byte[] fileReader = new byte[4096];
  
              long fileSize = body.contentLength();
              long fileSizeDownloaded = 0;
  
              inputStream = body.byteStream();
              outputStream = new FileOutputStream(futureStudioIconFile);
  
              while (true) {
                  int read = inputStream.read(fileReader);//going to exception with networkonmainthreadexception
  
                  if (read == -1) {
                      break;
                  }
  
                  outputStream.write(fileReader, 0, read);
  
                  fileSizeDownloaded += read;
  
              }
  
  //                outputStream.flush();
  
          } catch (Exception e) {
  
              Log.i(&quot;PDF&quot;, e.getMessage());
          } finally {
              safeClose(inputStream);
              safeClose(outputStream);
          }
  
  
          return futureStudioIconFile;
  
      }
  </code></pre>