[英]I need know how much bytes has been uploaded for update a progress bar android
[英]How to know how much percentage file uploaded to server in android
我正在使用此代码将文件从我的android上传到服务器,但我不知道我的文件上传了多少(例如30MB
从100MB
)! 有什么办法吗?
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.