簡體   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