1

I want to show progress bar while uploading a video from my app to php server.The below will upload the file to the server correctly.But i dont know how to show the progress bar.If anybody knows pls help me.

Here is my code:

 private void doFileUpload(){
            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 = 8*1024*1024;
            Cursor c = (MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name), new String[] {
                     (MainscreenActivity.COL_HwdXml)}, null, null, null, null,
                              null);
             if(c.getCount()!=0){
             c.moveToLast();
             for(int i=c.getCount()-1; i>=0; i--) {
                  value=c.getString(0);           
             }
             }
            String urlString = value+"/upload_file.php";
            try
            {
             //------------------ CLIENT REQUEST
                UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME",fname);

            FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            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=\"uploadedfile\";filename=\"" + fname + "."+extension+"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();
             System.out.println("BYTES:--------->"+bytesAvailable);
             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             System.out.println("BUFFER SIZE:--------->"+bufferSize);
             buffer = new byte[bufferSize];
             System.out.println("BUFFER:--------->"+buffer);
             bytesRead = fileInputStream.read(buffer,0,bufferSize);
             System.out.println("BYTES READ:--------->"+bytesRead);
             while (bytesRead > 0)
             {
              dos.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              System.out.println("RETURNED");
             }
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             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);
                  }
                  inStream.close();

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

3 Answers3

0

make this call before starting network comm...

ProgressDialog dialog = ProgressDialog.show(this, "", "Loading"); 

call the below after completion..

dialog.dismiss();

...

Its better to use AsyncTask for network communications

Noundla Sandeep
  • 3,334
  • 5
  • 29
  • 56
  • Iam new to android.iam not that much familiar with AsyncTask.If u have any code related to that please send me... –  Apr 16 '12 at 11:41
  • see [this](http://www.vogella.com/articles/AndroidPerformance/article.html). You will get more info....then you can develop as you own... – Noundla Sandeep Apr 16 '12 at 11:44
  • dont write any code that reflects UI in doInBackGround() method. Instead write in onPostExecute(). – Noundla Sandeep Apr 16 '12 at 11:47
  • cheers.......... If you got solution make as accepted answer. this stops more answers for this question. – Noundla Sandeep Apr 16 '12 at 11:50
  • Hi i want to show the progress bar in notification area..will it can be done using Asynctask? –  Apr 19 '12 at 07:50
  • see this link http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android – Noundla Sandeep Apr 19 '12 at 10:09
  • how can we show the progress bar while uploading the video in notification area and remove it after uploading –  Apr 19 '12 at 12:49
  • can we upload a 50MB video to the server from our application? –  Apr 22 '12 at 06:52
0

This question might help you. Basically you need to make a ProgressDialog in onPreExecute() method of AsyncTask and dismiss it in onPostExecute().
Upload code will go in the doInBackGround() method of AsyncTask.

Community
  • 1
  • 1
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • Iam new to android.iam not that much familiar with AsyncTask.If u have any code related to that please send me... –  Apr 16 '12 at 11:43
  • Here is my blog post which has a nice tutorial for AsyncTask - http://abhishek347.wordpress.com/2012/03/27/asynctask-on-rescue/ – 0xC0DED00D Apr 16 '12 at 12:41
  • Hi i want to show the progress bar in notification area..will it can be done using Asynctask? –  Apr 19 '12 at 07:50
  • check this - http://stackoverflow.com/questions/1871515/progress-bar-in-notification-bar-when-uploading-image – 0xC0DED00D Apr 19 '12 at 09:25
  • how can we show the progress bar while uploading the video in notification area and remove it after uploading –  Apr 19 '12 at 12:49
  • Check the question link I posted in earlier comment, Have u checked the answers? – 0xC0DED00D Apr 19 '12 at 13:10
  • i can show the progress bar in my application.but i want to show it in the notification bar –  Apr 19 '12 at 13:14
  • I got that, just check the link. It's about Showing Progressbar in notification area – 0xC0DED00D Apr 19 '12 at 13:18
  • can u post the exact link here? –  Apr 19 '12 at 13:57
  • http://stackoverflow.com/questions/1871515/progress-bar-in-notification-bar-when-uploading-image – 0xC0DED00D Apr 19 '12 at 13:58
  • can we upload a 50MB video to the server from our application? –  Apr 22 '12 at 06:51
  • You shouldn't ask all your question in the comments. Ask a new Question. – 0xC0DED00D Apr 22 '12 at 14:59
0

Call new loadVideo().execute(); where ever you needed. And add the following class to do the video loading.

public class loadVideo extends AsyncTask<Void, Void, Void>
    {

        private final ProgressDialog dialog = new ProgressDialog(
                YourActivity.this);
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }
        protected void onPostExecute(Void result) {

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }

                }
            });



        }

        @Override
        protected Void doInBackground(Void... params) {
 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 = 8*1024*1024;
            Cursor c = (MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name), new String[] {
                     (MainscreenActivity.COL_HwdXml)}, null, null, null, null,
                              null);
             if(c.getCount()!=0){
             c.moveToLast();
             for(int i=c.getCount()-1; i>=0; i--) {
                  value=c.getString(0);           
             }
             }
            String urlString = value+"/upload_file.php";
            try
            {
             //------------------ CLIENT REQUEST
                UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME",fname);

            FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            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=\"uploadedfile\";filename=\"" + fname + "."+extension+"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();
             System.out.println("BYTES:--------->"+bytesAvailable);
             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             System.out.println("BUFFER SIZE:--------->"+bufferSize);
             buffer = new byte[bufferSize];
             System.out.println("BUFFER:--------->"+buffer);
             bytesRead = fileInputStream.read(buffer,0,bufferSize);
             System.out.println("BYTES READ:--------->"+bytesRead);
             while (bytesRead > 0)
             {
              dos.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              System.out.println("RETURNED");
             }
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             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);
                  }
                  inStream.close();

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

            return null;
        }

    }
Manikandan
  • 1,479
  • 6
  • 48
  • 89
  • please could u tell me how to call the loadVideo class? Iam new to android manikandan. That's y iam asking –  Apr 16 '12 at 12:15
  • when the progress bar will get disappear here? –  Apr 16 '12 at 12:33
  • Hi i want to show the progress bar in notification area..will it can be done using Asynctask? –  Apr 19 '12 at 07:45
  • You can't show the progress bar in notification area using AsyncTask. follow this link http://united-coders.com/nico-heid/show-progressbar-in-notification-area-like-google-does-when-downloading-from-android – Manikandan Apr 19 '12 at 10:00
  • you can get the sample from this github site https://github.com/nheid/unitedcoders-android – Manikandan Apr 19 '12 at 10:02
  • how can we show the progress bar while uploading the video in notification area? –  Apr 19 '12 at 12:28
  • Can u add some code to display progress bar in the notification area and updating the progress bar? –  Aug 22 '12 at 14:19