user5868602
user5868602

Reputation:

How to know how much percentage file uploaded to server in android

I am using this code to upload file from my android to a server but I dont know how much of my file is uploaded( for example 30MB from 100MB)! Is there any way?

String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
    "yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(url);

InputStreamEntity reqEntity = new InputStreamEntity(
        new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...

} catch (Exception e) {
// show error
}

Upvotes: 0

Views: 867

Answers (2)

esmail jamshidiasl
esmail jamshidiasl

Reputation: 128

hello for working with internet you must to use new Thread but in thread we can't access main thread so make a new runonuithread and change progress from it

new Thread(new Runnable() {
        @Override
        public void run() {
            /*
            upload codes goes here
             */

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //set value of progressbar here
                }
            });
        }
    }).start();

Upvotes: 1

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20990

For that you have use AsyncTask and set ProgressDialog percent using publishProgress() method. For more detail visit this link.

Upvotes: 2

Related Questions