title: Uploading a Image Works on some devices but returns Stream FixedLengthSource .inputStream()
body: <p>I am developing an android application that uploads an image to a specified directory using PHP and okhttp .  Well , the frontend and backend work fine on my personal device , but it crashes on non-Samsung devices.</p>
  <p>When I dug into the problem I found out that the server returns :</p>
  <p>com.android.okhttp.internal.http.Http1xStream$FixedLengthSource@17768aa).inputStream()
  on the non-Samsung devices.</p>
  <p>The Java Code of the file Upload is as Follows :</p>
  <pre><code>public class UploadGalleryStoryPost {
      private Context context;
      private String fileName = &quot;&quot;, User_Id = &quot;&quot;,Type=&quot;&quot;;
      private ProgressDialog progressDialog;
      private int serverResponseCode = 0;
      private String count = &quot;&quot;;
      private String fileCount = &quot;&quot;;
  
      public UploadGalleryStoryPost(String fileName, Context context, String User_Id, String Type, String count, String fileCount) {
          this.fileName = fileName;
          this.context = context;
          this.User_Id = User_Id;
          this.Type = Type;
          this.count = count;
          this.fileCount = fileCount;
          new UploadingGalleryPost().execute();
  
      }
  
      private class UploadingGalleryPost extends AsyncTask&lt;Void, Void, Void&gt; {
  
          HttpURLConnection conn = null;
          DataOutputStream dos = null;
          String lineEnd = &quot;\r\n&quot;;
          String twoHyphens = &quot;--&quot;;
          String boundary = &quot;*****&quot;;
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize =1024 * 1024;
          File sourceFile_profile = new File(fileName);
          private String upLoadServerUri = APP_SERVER_URL+&quot;AddAStory.php&quot;;
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              progressDialog=new ProgressDialog(context);
              progressDialog.setMessage(&quot;Uploading...&quot;);
              progressDialog.setCancelable(false);
              progressDialog.show();
          }
  
          @Override
          protected void onPostExecute(Void result) {
              try{
                  if(serverResponseCode == 200){
                      int count1 = Integer.parseInt(count);
                      int fileCount1 = Integer.parseInt(fileCount);
                      Log.e(&quot;value&quot;, &quot; &quot;+ String.valueOf(count1-1)+&quot; &quot;+fileCount1);
                      if((fileCount1-1) == count1) {
                          AlertDialog.Builder builder1 = new AlertDialog.Builder(context, R.style.myDialog);
                          builder1.setCancelable(false);
                          builder1.setTitle(&quot;Alert!&quot;);
                          builder1.setMessage(&quot;Uploaded successfully.&quot;);
                          builder1.setPositiveButton(&quot;OK&quot;, new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  dialog.dismiss();
                                  Intent i = new Intent(context, DashBoard.class);
                                  context.startActivity(i);
                              }
                          });
                          AlertDialog dialog1 = builder1.create();
                          dialog1.show();
                      }
                      else {
                          progressDialog.dismiss();
                          Toast.makeText(context, fileName+&quot; has been uploaded successfully&quot;, Toast.LENGTH_SHORT).show();
                      }
                  }
              } catch (Exception e) {
  
                  e.printStackTrace();
                  Toast.makeText(context, &quot;Got Exception : see logcat &quot;, Toast.LENGTH_SHORT).show();
                  Log.e(&quot;UploadtoserverException&quot;, &quot;Exception : &quot;
                          + e.getMessage(), e);
              }
              super.onPostExecute(result);
          }
  
          @Override
          protected Void doInBackground(Void... voids) {
              try{
                  // open a URL connection to the Servlet
                  FileInputStream fileInputStream_profile = new FileInputStream(sourceFile_profile);
                  URL url = new URL(upLoadServerUri);
  
                  // Open a HTTP  connection to  the URL
                  conn = (HttpURLConnection) url.openConnection();
                  conn.setDoInput(true); // Allow Inputs
                  conn.setDoOutput(true); // Allow Outputs
                  conn.setUseCaches(false); // Don't use a Cached Copy
                  conn.setRequestMethod(&quot;POST&quot;);
  //                conn.setChunkedStreamingMode(1024);
                  conn.setRequestProperty(&quot;Connection&quot;, &quot;Keep-Alive&quot;);
                  conn.setRequestProperty(&quot;ENCTYPE&quot;, &quot;multipart/form-data&quot;);
                  conn.setRequestProperty(&quot;Content-Type&quot;, &quot;multipart/form-data;boundary=&quot; + boundary);
                  conn.setRequestProperty(&quot;Attachment&quot;, fileName);
                  dos = new DataOutputStream(conn.getOutputStream());
  
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes(&quot;Content-Disposition: form-data; name=\&quot;Attachment\&quot;;filename=\&quot;&quot;+fileName+&quot;\&quot;&quot; + lineEnd);
                  dos.writeBytes(lineEnd);
  
                  // create a buffer of  maximum size
                  bytesAvailable = fileInputStream_profile.available();
  
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];
  
                  // read file and write it into form...
                  bytesRead = fileInputStream_profile.read(buffer, 0, bufferSize);
  
                  while (bytesRead &gt; 0) {
  
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fileInputStream_profile.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fileInputStream_profile.read(buffer, 0, bufferSize);
  
                  }
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
  
  
  
                  dos.writeBytes(&quot;Content-Disposition: form-data; name=\&quot;User_Id\&quot;&quot; + lineEnd);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(User_Id);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
  
                  dos.writeBytes(&quot;Content-Disposition: form-data; name=\&quot;Type\&quot;&quot; + lineEnd);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(Type);
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  
  
                  // Responses from the server (code and message)
                  serverResponseCode = conn.getResponseCode();
                  String serverResponseMessage = conn.getResponseMessage();
                  conn.getErrorStream();
  
                  Log.e(&quot;uploadFile&quot;, &quot;HTTP Response is : &quot;
                          + serverResponseMessage + &quot;: &quot; + serverResponseCode+&quot; &quot;+ conn.getErrorStream());
  
                  //close the streams //
                  fileInputStream_profile.close();
                  dos.flush();
                  dos.close();
              }
              catch (MalformedURLException ex) {
  
                  ex.printStackTrace();
  
                  Log.e(&quot;Upload file to server&quot;, &quot;error: &quot; + ex.getMessage(), ex);
              } catch (Exception e) {
  
                  e.printStackTrace();
  
                  Log.e(&quot;UploadtoserverException&quot;, &quot;Exception : &quot;
                          + e.getMessage(), e);
              }
              return null;
          }
      }
  }
  </code></pre>
  <p>The php code is :</p>
  <pre><code>&lt;?php
  date_default_timezone_set('Asia/Kolkata');
  $date = date('Y-m-d H:i:s');
  $new_time = date(&quot;Y-m-d H:i:s&quot;, strtotime('+24 hours'));
  $day = date(&quot;l&quot;);
  $response = array();
  include 'db_connect.php';
  if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {
      $User_Id = $_POST['User_Id'];
      $Story_Content = $_POST['Story_Content'];
      $B_Id = $_POST['B_Id'];
      $Type = $_POST['Type'];
  
      if($Type == 'BGTXT'){
          $sql = &quot;INSERT INTO Story_Common(User_Id,Timestamp_Start,Status, Timestamp_End)
          VALUES('$User_Id','$date','Open','$new_time')&quot;;
          $result = sqlsrv_query($conn, $sql);
  
          if($result){
              $sql1 = &quot;SELECT TOP 1 * FROM Story_Common ORDER BY Story_Id DESC&quot;;
              $res1 = sqlsrv_query($conn, $sql1);
              if (sqlsrv_has_rows($res1) == true)
              {
                  while ($row_sql1 = sqlsrv_fetch_array($res1, SQLSRV_FETCH_BOTH))
                  {
                      $Story_Id = $row_sql1[&quot;Story_Id&quot;];
                  }
              }
  
              $sql2 = &quot;INSERT INTO Story(Story_Id,Story_Content,B_Id)
              VALUES('$Story_Id',N'$Story_Content','$B_Id')&quot;;
              $result2 = sqlsrv_query($conn, $sql2);
  
              if($result2){
                  $response['success'] = 200;
                  $response['message'] = &quot;Insert in db success.&quot;;
              }
              else{
                  $response['success'] = 0;
                  $response['message'] = &quot;Failed to insert db.&quot;;
              }
          }
          else{
              $response['success'] = 0;
              $response['message'] = &quot;Failed to insert db.&quot;;
          }
  
      }
      else if($Type == 'Photo/Video'){
          $sql = &quot;INSERT INTO Story_Common(User_Id,Timestamp_Start,Status,Timestamp_End)
          VALUES('$User_Id','$date','Open','$new_time')&quot;;
          $result = sqlsrv_query($conn, $sql);
  
          if($result){
  
              if (empty($_FILES[&quot;Attachment&quot;][&quot;name&quot;])) {
                  $path = &quot;NA&quot;;
              } 
              else {
                  $Attachment=$_FILES[&quot;Attachment&quot;][&quot;name&quot;];
                  $temp=$_FILES[&quot;Attachment&quot;][&quot;tmp_name&quot;];
                  $tst= time();
                  $url = &quot;Post_Media/&quot; . $tst . $Attachment;
                  $path=&quot;http://kidsfb.kidsfb.com/ver1PHP/&quot;.$url;
                  move_uploaded_file($temp,$url);
              }
  
              $sql1 = &quot;SELECT TOP 1 * FROM Story_Common ORDER BY Story_Id DESC&quot;;
              $res1 = sqlsrv_query($conn, $sql1);
              if (sqlsrv_has_rows($res1) == true)
              {
                  while ($row_sql1 = sqlsrv_fetch_array($res1, SQLSRV_FETCH_BOTH))
                  {
                      $Story_Id = $row_sql1[&quot;Story_Id&quot;];
                  }
              }
  
              $sql2 = &quot;INSERT INTO Story_Media(Story_Id,Media_Att)
              VALUES('$Story_Id','$path')&quot;;
              $result2 = sqlsrv_query($conn, $sql2);
  
              if($result2){
                  $response['success'] = 200;
                  $response['message'] = &quot;Insert in db success.&quot;;
              }
              else{
                  $response['success'] = 0;
                  $response['message'] = &quot;Failed to insert db.&quot;;
              }
          }
          else{
              $response['success'] = 0;
              $response['message'] = &quot;Failed to insert db.&quot;;
          }
      }
  
      echo json_encode($response);
  }
  ?&gt;
  
  </code></pre>
  <p>Tried increasing the buffer size to 2048 from 1024 still doesn't work on other devices.</p>