简体   繁体   中英

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
}

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM