android 5.0.1 file upload not throwing exception when network is lost

79 views Asked by At

I have an Android intentservice for uploading files to server. The files will be added to the intentservice queue for every 1 min.It is working fine in the android devices with OS less than 5.0.1. But in android 5.0.1 while uploading the file, if network is lost, I am not getting any response, not throwing any exception and the intent service is blocked. Files are not uploading from that point. I have to restart the app in order to make the service work again. Below is the code I have in onHandleIntent method.

            int ReadBytes = 0;
            int BUFSIZ = 4096;
            byte Buf[] = new byte[BUFSIZ];
            outputStream = conn.getOutputStream();
            dataOutputStream =  new DataOutputStream(outputStream);
            fileInputStream = new FileInputStream(source);
            long prevBytesRead = 0;
            ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
            while(ReadBytes>0){
                dataOutputStream.write(Buf, 0, ReadBytes);
                prevBytesRead += ReadBytes;
                long totalbytes = fileInputStream.getChannel().size();
                ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
            }

            outputStream.flush();
            dataOutputStream.flush();

Any help would be appreciated.

1

There are 1 answers

2
jlhonora On

You are probably leaking the stream, you should try wrapping it in a try/catch block:

try {
      int ReadBytes = 0;
      int BUFSIZ = 4096;
      byte Buf[] = new byte[BUFSIZ];
      outputStream = conn.getOutputStream();
      dataOutputStream =  new DataOutputStream(outputStream);
      fileInputStream = new FileInputStream(source);
      long prevBytesRead = 0;
      ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
      while(ReadBytes>0){
          dataOutputStream.write(Buf, 0, ReadBytes);
          prevBytesRead += ReadBytes;
          long totalbytes = fileInputStream.getChannel().size();
          ReadBytes = fileInputStream.read(Buf, 0, BUFSIZ);
      }

      outputStream.flush();
      dataOutputStream.flush();
} catch (IOException e) {

} finally {
      // Close streams with an additional try/catch
}

Perhaps you should take a look at Facebook's infer tool: fbinfer.com , very useful for these situations.