[英]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上传到服务器,但我不知道我的文件上传了多少(例如
30MB
从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
}
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.