Skip to content Skip to sidebar Skip to footer

Show Upload Progress For Json

I have the following piece of code and I would want to add a feature in here such that I know when the JSON content is uploaded. I am uploading a JSON content. @Override protected

Solution 1:

Initiate your progress bar as

private ProgressDialog pDialog;

On onPreExecute()

@OverrideprotectedvoidonPreExecute() {
            // TODO Auto-generated method stubsuper.onPreExecute();

            pDialog = newProgressDialog(getParent());
            pDialog.setMessage("Please wait ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

On onPostExecute()

protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
pDialog.dismiss();
}

For showing progress bar with percentage try ..

publicclassAndroidDownloadFileByProgressBarActivityextendsActivity {

    // button to show progress dialog
    Button btnShowProgress;

    // Progress Dialogprivate ProgressDialog pDialog;
    ImageView my_image;
    // Progress dialog type (0 - for Horizontal progress bar)publicstaticfinalintprogress_bar_type=0; 

    // File url to downloadprivatestaticStringfile_url="your_url";

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // show progress bar button
        btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
        // Image view to show image after downloading
        my_image = (ImageView) findViewById(R.id.my_image);
        /**
         * Show Progress bar click event
         * */
        btnShowProgress.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View v) {
                // starting new Async TasknewDownloadFileFromURL().execute(file_url);
            }
        });
    }

    /**
     * Showing Dialog
     * */@Overrideprotected Dialog onCreateDialog(int id) {
        switch (id) {
        case progress_bar_type:
            pDialog = newProgressDialog(this);
            pDialog.setMessage("Downloading file. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(true);
            pDialog.show();
            return pDialog;
        default:
            returnnull;
        }
    }

    /**
     * Background Async Task to download file
     * */classDownloadFileFromURLextendsAsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */@OverrideprotectedvoidonPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */@Overrideprotected String doInBackground(String... f_url) {
            int count;
            try {
                URLurl=newURL(f_url[0]);
                URLConnectionconection= url.openConnection();
                conection.connect();
                // getting file lengthintlenghtOfFile= conection.getContentLength();

                // input stream to read file - with 8k bufferInputStreaminput=newBufferedInputStream(url.openStream(), 8192);

                // Output stream to write fileOutputStreamoutput=newFileOutputStream("/sdcard/downloadedfile.jpg");

                byte data[] = newbyte[1024];

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

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            returnnull;
        }

        /**
         * Updating progress bar
         * */protectedvoidonProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
       }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/@OverrideprotectedvoidonPostExecute(String file_url) {
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);

            // Displaying downloaded image into image view// Reading image path from sdcardStringimagePath= Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg";
            // setting downloaded into image view
            my_image.setImageDrawable(Drawable.createFromPath(imagePath));
        }

    }
}

Post a Comment for "Show Upload Progress For Json"