3

While uploading multiple image at a time i want to show progress bar which show the progress bar in statusbar notification area with the info 1/5 and 2/5 and so on. where 1 is no of image uploaded and 5 is total no of image to be uploaded.

here i am able show progress bar in notification area. can any one suggest me, how to calculate no of image uploaded(finished) to show in progress bar (like 1/5)update. thanks in advance.

For making more clear

i have a asyntask which upload a single image to server. but i am not able to do

1> calculate size of total image (say for example 5 image)

2>how to find no of image uploaded in total 5 image

private class FileUploadTask extends AsyncTask<Object, Integer,String> {

    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(context);
        dialog.setMessage("Uploading...");
        dialog.setIndeterminate(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setProgress(0);
        dialog.show();
    }

    @Override
    protected String doInBackground(Object... arg0) {
        try {
            File file = new File("/mnt/sdcard/DCIM/100MEDIA/IMAG0149.jpg");
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];
            fileInputStream.read(bytes);
            fileInputStream.close();

            URL url = new URL("http://android.com.bd/form.php");
            HttpURLConnection connection = 
                    (HttpURLConnection) url.openConnection();
            OutputStream outputStream = connection.getOutputStream();

            int bufferLength = 1024;
            for (int i = 0; i < bytes.length; i += bufferLength) {
                int progress = (int)((i / (float) bytes.length) * 100);
                Log.i("progress",progress+"dfdf");
                publishProgress(progress);
                if (bytes.length - i >= bufferLength) {
                    outputStream.write(bytes, i, bufferLength);
                } else {
                    outputStream.write(bytes, i, bytes.length - i);
                }
            }
            publishProgress(100);

            outputStream.close();
            outputStream.flush();

            InputStream inputStream = connection.getInputStream();
            // read the response
            inputStream.close();
            return "ok";

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        dialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        Log.v("sds", result);
        try {
            dialog.dismiss();
        } catch(Exception e) {
        }

    }

}
Krishna Shrestha
  • 1,662
  • 14
  • 38
  • try this hope help http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660 – Deepak Swami Sep 11 '12 at 09:05
  • Check the following answer, it may help: [http://stackoverflow.com/questions/15572747/progressbar-in-asynctask-is-not-showing-on-upload][1] [1]: http://stackoverflow.com/a/19295719/463846 – Mousa Oct 10 '13 at 12:38

3 Answers3

4

Take a look at this TL;DR blog post/tutorial. You should be able to do something similar. You'll want to use a ProgressDialog, updating its state using an ASyncTask. If you're already using an ASyncTask for your image upload, you already have the pieces in place.

http://toolongdidntread.com/android/android-multipart-post-with-progress-bar/

Also take a look at this SO question - Download a file with Android, and showing the progress in a ProgressDialog. Your question has been answered before. You'll just need to adapt the solution to display the progress bar at 1/5, 2/5, etc by customizing onProgressUpdate. I haven't tested this code, but I'd imagine something along these lines will allow you to display the progress incrementally like you want.

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        if (progress[0] < 20) {            
            mProgressDialog.setProgress(0);
        } else if (progress[0] < 40) {            
            mProgressDialog.setProgress(20);
        }
        else if (progress[0] < 60) {            
            mProgressDialog.setProgress(40);
        }
        else if (progress[0] < 80) {            
            mProgressDialog.setProgress(60);
        }
        else if (progress[0] < 100) {            
            mProgressDialog.setProgress(80);
        }
        else if (progress[0] == 100) {            
            mProgressDialog.setProgress(100);
        }
    }
Community
  • 1
  • 1
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
  • As in, does the page load or does the code sample work? Both work for me. – Kyle Clegg Sep 11 '12 at 19:07
  • thanks @Kyle second link is really useful but not the solution. yes i am able to upload single image to server with progress update but i have no idea how to upload multiple image at time. – Krishna Shrestha Sep 12 '12 at 04:11
  • +1 for info. can you suggest me how multiple image can be uploaded. – Krishna Shrestha Sep 12 '12 at 04:19
  • I'm not sure how you could do the multiple image uploads all at the same time, but I would suggest doing it sequentially. For example you could fire off the next upload in onReceiveResult or onPostExecute and continue until all images have been uploaded. You'd have to refactor your progress dialog to handle this so that you wouldn't get a new progress dialog for each upload. – Kyle Clegg Sep 13 '12 at 20:22
0

Where are your images? you have to do like

File fav = new File(Environment.getExternalStorageDirectory().getPath());
File[] filesav = fav.listFiles();
for (int i = 0; i < filesav.length; i++) {
        inside you send your pictures and count 

}

the variable i is your image number and filesav.length is your total image number

paponi
  • 61
  • 1
  • 6
-1

Well, it really depends on how you download you images ?

What I would advice is to create an instance (singleton) of some DownloadManager class, which would count and manage the number of started and finished download. This instance will be used to create new downloads, and will be notified each time a download is finished. When notified, it could then update the progress bar.

But this is a very generic answer. Cannot do better without more informations on how you currently do the downloads (are they done in separate threads ? Are they sequential or parallel ?...)

Orabîg
  • 11,718
  • 6
  • 38
  • 58