Progressbar issue while uploading a file in SDCard of the mobile device to a file server
<p>Implemented the functionality of file(image , text , etc) upload(from SD card of the mobile device) to a file server location from a mobile application via SIM card / WiFi using the following code.</p>

<p>File upload functionality is working fine with the following code , only the update of the progressdialog is an issue here.</p>

<p><strong>uploadFile() is called from doInBackground(String... params)  of AsyncTask</strong></p>

<p><strong>protected void onProgressUpdate(String... values) handles the progressdialog percentage update</strong></p>

<p>The following code is responsible for file upload</p>

<p>Problem faced with the following code:</p>

<p>i> The progress dialog updates from 0% to 100% with proper intervals &amp; then waits for a long time ,before the file status (boolean value) is returned.</p>

<pre><code>protected boolean uploadFile(String serverUrl, String filePath) {
            HttpURLConnection connection = null;
            DataOutputStream outputStream = null;
            DataInputStream inputStream = null;
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int serverResponseCode;
            String serverResponseMessage;
            boolean uploadstatus = false;
            int count;
            long lengthOfFile;

            try {
                FileInputStream fileInputStream;
                fileInputStream = new FileInputStream(new File(filePath));
                URL url = new URL(serverUrl);
                connection = (HttpURLConnection) url.openConnection();
                // Allow Inputs &amp; Outputs
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setUseCaches(false);
                // Enable POST method
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                outputStream = new DataOutputStream(connection.getOutputStream());
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream.writeBytes("Content-Disposition: form-data; name=\"upload\";filename=\""
                        + Utils.getInstance().getFileName(filePath) + "\"" + lineEnd);
                outputStream.writeBytes(lineEnd);
                lengthOfFile = new File(filePath).length();// length of file
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[1024];
                bytesRead = 0;
                String progressMsg = "";
                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    total += bytesRead;
                    progressMsg = new StringBuffer("").append((int) ((total * 100) / totalLengthOfFile))
                            .toString();
                    prgressBarMsg[0] = progressMsg;
                    publishProgress(prgressBarMsg);
                    outputStream.write(buffer);
                }
                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // Responses from the server (code and message)
                serverResponseCode = connection.getResponseCode();
                serverResponseMessage = connection.getResponseMessage();
                if (serverResponseCode == 200)// HTTP OK Message from server
                {
                    uploadstatus = true;
                } else {
                    uploadstatus = false;
                }
                catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                connection.disconnect();
            }
            return uploadstatus;
        }
</code></pre>

<p>It seems to me that the the percentage displayed on the progressdialog is actually the percentage data read from the file (located on the SDCard) to the buffer &amp; not the data which is sent to the file server from the mobile device via SIM card / WiFi. The long delay after the display of 100% in progressdialog is the reason for my concern.</p>

<p>Kindly confirm if the source code shows a proper approach of update of progressdialog. Also the progressdialog in horizontal style shows both the values <strong>100 %</strong> &amp; <strong>100/100</strong> . </p>

<p>How to remove the display of 100/100 ?</p>

<p>Any alternate hints/suggestions are welcome.</p>
