0

I'm uploading a file and want to display a progress dialog to see the upload progress. I already included a horizontal dialog which works incorrectly, because it goes to 100% in 1 second and then it waits (because it's still uploading).

I used the following function for uploading:

 private void doFileUpload(String selectedPath){
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;
    String responseFromServer = "";
    String urlString = "https://xxxxxxxxxx.com";
    try
    {
        //------------------ CLIENT REQUEST
        File f = new File(selectedPath);
        FileInputStream fileInputStream = new FileInputStream(f);

        //calculate size of file
        wholeFilesize = f.length(); //returns the length in Bytes

        // open a URL connection to the Servlet
        URL url = new URL(urlString);
        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();
        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);
        // Don't use a cached copy.
        conn.setUseCaches(false);
        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
        dos = new DataOutputStream( conn.getOutputStream() );
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"upfile\";filename=\"" + selectedPath + "\"" + lineEnd);

        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        // read file and write it into form...
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            totalReadFilesize+=bytesRead;
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            int progress = (int)((totalReadFilesize*100)/(wholeFilesize));
            //Log.i("Progress: ", (int)((totalReadFilesize*100)/(wholeFilesize))+"");
            dialog.setProgress(progress);
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        }
        //publishProgress(100);
        Log.i("Size: ", wholeFilesize+"");
        Log.i("Bytes read: ", totalReadFilesize+"");

        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        Log.e("Debug","File is written");
        fileInputStream.close();
        dos.flush();
        dos.close();
    }
    catch (MalformedURLException ex)
    {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe)
    {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }
    //------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream ( conn.getInputStream() );
        String str;

        while (( str = inStream.readLine()) != null)
        {
            Log.e("Debug","Server Response "+str);
            JSONObject jsonObj = new JSONObject(str);
            //String status=(String) jsonObj.get("status");
            Log.i("TEST", (String) jsonObj.get("message"));
            uploadResultMessage = (String) jsonObj.get("message");
            Log.i("TEST", uploadResultMessage);

        }
        inStream.close();

    }
    catch (IOException ioex){
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Does someone know, how should I modify the code to see the real progress of the file, which is being uploaded? I find it strange, because I'm doing the same as the user in his doInBackground() in this thread.

Community
  • 1
  • 1
TheAptKid
  • 1,559
  • 3
  • 25
  • 47
  • 3
    chek this link [click here](http://www.androidhive.info/2012/04/android-downloading-file-by-showing-progress-bar/) – Pankaj Jun 02 '15 at 07:50
  • I hope you are not doing this in UI thread. If so, the while loop will block the UI until it is done. – vilpe89 Jun 02 '15 at 07:52
  • @vilpe89: Don't worry. I'll use a non-ui thread for it. – TheAptKid Jun 02 '15 at 08:37
  • if it's in a non-ui thread how could you interact with UI? The `publishProgress` allows in an AsyncTask to communicate with the UI thread. – Groco Jun 02 '15 at 09:02
  • have to set the max size of progressbar dialog.setMax(maxvalue)? – SAM Jun 02 '15 at 10:59

0 Answers0