How to show progress bar status by percentage while uploading json data?

前端 未结 5 1434
旧巷少年郎
旧巷少年郎 2020-12-20 15:55

I am uploading string and photo.and its working fine. Now I want to show progress bar while uploading data with percentage but percentage show very quickly to 100 percentage

5条回答
  •  春和景丽
    2020-12-20 16:52

    protected class upload_images extends AsyncTask {
        ProgressDialog progressDialog;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // showDialog(progress_bar_type);
            progressDialog = new ProgressDialog(Accept_Report.this);
            progressDialog.setCancelable(false);
            //  dialog.setCanceledOnTouchOutside(false);
            progressDialog.setIndeterminate(false);
            //  progressDialog.setMax(100);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            // progressDialog.setProgress(0);
            progressDialog.setMax(100);
            // progressDialog.setMessage("Loading ...");
            progressDialog.show();
            //  ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar2);
        }
    
        @Override
        protected String doInBackground(String... params) {
            URL url;
            HttpURLConnection connection = null;
            String http = Util.URL + "reports/media/create";
            try {
                url = new URL(http);
                connection = (HttpURLConnection) url.openConnection();
                .
                .
                .
                connection.connect();
    
                ...
    
                // you are doing this
    
                // what is jsonParam ?
                //byte[] payload = jsonParam.toString().getBytes("UTF-8");
                // how you gonna get content lenght from it?
    
                int count = 0;
                OutputStream wr = connection.getOutputStream();
                InputStream inputStream = null;
                byte[] payload = jsonParam.toString().getBytes("UTF-8");
                int totalSze = payload.length;
                Log.e("Total size ", "" + totalSze);
                int bytesTransferred = 0;
                int chunkSize = (2 * totalSze) / 100;
                boolean last_loop = false;
                // publishProgress(0);
    
                ...
                // Do like this example
                // getting file length
                int lenghtOfFile = connection.getContentLength();
                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(), 8192);
                // Output stream to write file
                OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");
    
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                    publishProgress((int) ((total * 100) / lenghtOfFile));
    
                    // writing data to file
                    output.write(data, 0, count);
                }
    
    
            } catch (Exception e) {
            }
            return null;
        }
    
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            //  Log.e("dfsf",""+values[0]);
            progressDialog.setProgress(values[0]);
            //   progressDialog.setProgress(values[0]);
    
        }
    
        @Override
        protected void onPostExecute(String result) {
            if (HttpResultimage == 204) {
                progressDialog.dismiss();
            }
        }
    }
    

提交回复
热议问题