![](/img/trans.png)
[英]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.