Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }

}
like image 648
Krishna Shrestha Avatar asked Jan 26 '26 14:01

Krishna Shrestha


1 Answers

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);
        }
    }
like image 51
Kyle Clegg Avatar answered Jan 28 '26 06:01

Kyle Clegg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!