0

hi im working on an android messenger and i need to show the progress bar when sending and receiving files. could anyone help? for instance this is how i send the file,

@Override
    public boolean sendFile(String path,String ip, int port) {
    // TODO Auto-generated method stub


    try {


        String[] str = ip.split("\\.");

        byte[] IP = new byte[str.length];

        for (int i = 0; i < str.length; i++) {

            IP[i] = (byte) Integer.parseInt(str[i]);


        }
        Socket socket = getSocket(InetAddress.getByAddress(IP), port);
        if (socket == null) {
            Log.i("SO sendFILE","null");

            return false;
        }

        Log.i("SocketOP", "sendFILE-1");
        File  f = new File(path);


        BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );

        FileInputStream fileIn = new FileInputStream(f);
        Log.i("SocketOP", "sendFILE-2");
        byte [] buffer  = new byte [(int)f.length()];
        System.out.println("SO sendFile f.length();" + f.length());
        int bytesRead =0;
        while ((bytesRead = fileIn.read(buffer)) > 0) {
            out.write(buffer, 0, buffer.length);
            System.out.println("SO sendFile" + bytesRead);
        }
        out.flush();
        out.close();
        fileIn.close();
        Log.i("SocketOP", "sendFILE-3");






    } catch (IOException e) {
        return false;           
        //e.printStackTrace();
    }
    //  Toast.makeText(this, "Lvbvhhging...", Toast.LENGTH_SHORT).show();

    return true;        
}
    }

now where do i put the bar and how do i do that inorder to show the user the progress?

hardy.exe
  • 35
  • 1
  • 12

3 Answers3

2

try this ::

 private class xyz extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(tranning.this);

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Please Wait...");
            this.dialog.show();
            // put your code which preload with processDialog  
        }


        @Override
        protected Void doInBackground(Void... arg0) {
            // put your code here
Log.i("SocketOP", "sendFILE-1");
        File  f = new File(path);


        BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );

        FileInputStream fileIn = new FileInputStream(f);
        Log.i("SocketOP", "sendFILE-2");
        byte [] buffer  = new byte [(int)f.length()];
        System.out.println("SO sendFile f.length();" + f.length());
        int bytesRead =0;
        while ((bytesRead = fileIn.read(buffer)) > 0) {
            out.write(buffer, 0, buffer.length);
            System.out.println("SO sendFile" + bytesRead);
        }
        out.flush();
        out.close();
        fileIn.close();
            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
              this.dialog.dismiss();

            }   
        }
    }

and use this in main ::

    new xyz().execute();
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • Thanx for the kind words. But how you be a fan? – Paresh Mayani Sep 09 '11 at 10:24
  • well u see the thing is, i call the sendFile method from another class. like my messaging activity has a chat box right, so i click the menu and select the sendfile option.now the sendfile method is called that contains the code that i put in my question. now do i need to make another class for that sendfile method to hold ? or do i just put the code that u suggested as it is? – hardy.exe Sep 09 '11 at 10:26
  • fan >> mean of programming world & with your post – Nikunj Patel Sep 09 '11 at 10:27
  • @hardy.exe you can continue with as it is – Nikunj Patel Sep 09 '11 at 10:30
  • okay thank you very much. one quick question : where do i call xyz().execute? the UI from where m calling the sendFile method? – hardy.exe Sep 09 '11 at 10:42
  • please have a look at my edited question. this is actually the complete code. – hardy.exe Sep 09 '11 at 10:54
  • hi, please could you show me how to add the bar now? i have a method inside the xyz class now..?? anyone? – hardy.exe Sep 10 '11 at 20:57
0

Do your file sending task in separate thread not in the UI thread. When you start this thread call your progress dialog and remvove it from thread when you are done using Handler.

For Handler you can refer this doc:

http://developer.android.com/reference/android/os/Handler.html

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
0
myProgressBar.setProgress(0);
while ((bytesRead = fileIn.read(buffer)) > 0) {
            out.write(buffer, 0, buffer.length);
            //get the previous value of progress bar 
            int old_value = myProgressBar.getProgress();
            //calculate how much did you read from the file
            int new_read =(int)( ((float) f.length() / bytesRead) )*100 ) ;
            //add the new read to the old_value
            int value = new_read+old_value;
            myProgressBar.setProgress(value); 
            System.out.println("SO sendFile" + bytesRead);
        }

see this link for how to construct a ProgressBar in android

confucius
  • 13,127
  • 10
  • 47
  • 66