繁体   English   中英

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

[英]How to know how much percentage file uploaded to server in android

我正在使用此代码将文件从我的android上传到服务器,但我不知道我的文件上传了多少(例如30MB100MB )! 有什么办法吗?

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
}

为此,您使用AsyncTask并使用publishProgress()方法设置ProgressDialog百分比。 有关详细信息,请访问此链接

你好,你必须使用新的线程,但在线程中,我们无法访问主线程,所以创建一个新的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