简体   繁体   English

如何知道在android中上传到服务器的文件百分比

[英]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 )! 我正在使用此代码将文件从我的android上传到服务器,但我不知道我的文件上传了多少(例如30MB100MB )! 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. 为此,您使用AsyncTask并使用publishProgress()方法设置ProgressDialog百分比。 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 你好,你必须使用新的线程,但在线程中,我们无法访问主线程,所以创建一个新的runonuithread并从中更改进度

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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