Question

I want to add progress bar to android project to show upload files. here is my code for upload images to server. postFile() is working fine. How to visible progress bar?

I want to add progressbar here.pleas see this code in the class

 //Progrssbar  Visible  here                     
    postFile();
 //Progrssbar  Invisible  here 

Here is the class

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


import android.os.AsyncTask;
import android.util.Log;


public class PostDataAsyncTask extends AsyncTask<String, String, String> {

        public String DataSendingTo="http://www.mysite.com/receiver.php/Incoming_Data_Receive";
        public String txtDescription;
        public static String[] FileNameStrings = new String[8];

        protected void onPreExecute() {
            super.onPreExecute();
            // do stuff before posting data
        }

        @Override
        protected String doInBackground(String... strings) {
            try 
            {
                //Progrssbar  Visible  here                     
                 postFile();
                //Progrssbar  Invisible  here 

            } catch (NullPointerException e) {

                e.printStackTrace();
            } catch (Exception e) {
                 e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String lenghtOfFile) {
            // do stuff after posting data
        }

    private void postFile(){
        try{

            // the file to be posted
            for(int f=0;f <FileNameStrings.length;f++)
            {

                String textFile =  FileNameStrings[f];
                if(textFile==null)
                {
                    break;
                }

                Log.v("Sending", "textFile: " + textFile);

                // the URL where the file will be posted

                Log.v("Sending", "postURL: " + DataSendingTo);

                // new HttpClient
                HttpClient httpClient = new DefaultHttpClient();

                // post header
                HttpPost httpPost = new HttpPost(DataSendingTo);

                File file = new File(textFile);
                FileBody fileBody = new FileBody(file);

                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                reqEntity.addPart("file", fileBody);
                httpPost.setEntity(reqEntity);

                // execute HTTP post request
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {

                    String responseStr = EntityUtils.toString(resEntity).trim();
                    Log.v("Sending", "Response: " +  responseStr);


                    //wait(1500);

                }
            }

        } catch (NullPointerException e) {
            e.printStackTrace();
            //Toast.makeText(getBaseContext(), "Error:1 on uplod file", Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            e.printStackTrace();
            //Toast.makeText(getBaseContext(), "Error:2 File may be already exists", Toast.LENGTH_LONG).show();

        }
    }

}

Was it helpful?

Solution

Add below code to your AsyncTask

class PostDataAsyncTask extends AsyncTask<String, String, String> {

    private ProgressDialog mDialog = null; 

    public PostDataAsyncTask(Context activityContext) {
        mDialog = new ProgressDialog(activityContext);
    }

    protected void onPreExecute() {
        super.onPreExecute();

        mDialog.setMessage("Wait...");
        mDialog.setCancelable(false); // Depends on app behaviour
        mDialog.show();
    }

    @Override
    protected String doInBackground(String... strings) {
        // Same as your implementation
    }

    @Override
    protected void onPostExecute(String lenghtOfFile) {
        if (mDialog != null)
            mDialog.dismiss();
    }
}

And call this as

PostDataAsyncTask postDataAsyncTask = new PostDataAsyncTask(Youractivity_name.this);
postDataAsyncTask.execute(With_your_params);

OTHER TIPS

you can se ein this example just call progressdialog in onPreExecute() method.,and refer this example.

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** Reference to the view which should receive the image */
private final WeakReference imageRef;public DownloadImageTask(ImageView imageView) {
    imageRef = new WeakReference(imageView);
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = ProgressDialog.show(DownloadImageActivity.this, "Wait", "Downloading...");
}

@Override
protected Bitmap doInBackground(String... params) {
    InputStream input = null;
    try {
        URL url = new URL(params[0]);
        // We open the connection
        URLConnection conection = url.openConnection();
        conection.connect();
        input = new BufferedInputStream(url.openStream(), 8192);
        // we convert the inputStream into bitmap
        bitmap = BitmapFactory.decodeStream(input);
        input.close();
    } catch (Exception e) {
        Log.e("Error: ", e.getMessage());
    }
    return bitmap;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(Bitmap bitmap) {
    progressDialog.dismiss();
    if (isCancelled()) {
        bitmap = null;
    }

    if (imageRef != null) {
        ImageView imageView = imageRef.get();
        if (imageView != null &amp;&amp; bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else
            Toast.makeText(DownloadImageActivity.this, "Error while downloading the image!", Toast.LENGTH_LONG).show();
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top