【问题标题】:How to know how much percentage file uploaded to server in android如何知道有多少百分比文件上传到android中的服务器
【发布时间】:2016-07-17 10:49:31
【问题描述】:

我正在使用此代码将文件从我的 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
}

【问题讨论】:

    标签: android file-upload httpclient progressdialog progress


    【解决方案1】:

    为此,您使用AsyncTask 并使用publishProgress() 方法设置ProgressDialog 百分比。更多详情请访问link

    【讨论】:

      【解决方案2】:

      您好,使用互联网,您必须使用新线程,但在线程中我们无法访问主线程,因此创建一个新的 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-08
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        • 1970-01-01
        相关资源
        最近更新 更多