Krishna Shrestha
Krishna Shrestha

Reputation: 1662

android upload multiple image with progress bar with progress 1 /5,2/5 and so on?

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) {
        }

    }

}

Upvotes: 3

Views: 7035

Answers (3)

paponi
paponi

Reputation: 61

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

Upvotes: 0

Kyle Clegg
Kyle Clegg

Reputation: 39470

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);
        }
    }

Upvotes: 4

Orab&#238;g
Orab&#238;g

Reputation: 11992

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 ?...)

Upvotes: -1

Related Questions