2

我有以下代码AsyncTask。文件上传得很好,我正在尝试实现一个进度条。我遇到的问题是它从 0 开始,然后就跳到 100。

我哪里做错了?

public class UploadingAsync extends AsyncTask<String, Integer, Object> {

  ProgressDialog dialog;
  BaseActivity base;

  public FileUploadAsyncTask(BaseActivity base) {
    this.base = base;
  }

  @Override
  protected Object doInBackground(String... path) {
    try {
      DroplrApp api = ((DroplrApplication) base.getApplication()).getDroplrAppApi();

      File file = new File(path[0]);

      Log.e("File uploaded", file.getAbsolutePath().toString());

      api.upload(file, new DroplrAppProgressListener() {

        @Override
        public void transferred(long transfer, long total) {
          publishProgress((int) (total * 100 / transfer));
        }
      });
    } catch (DroplrAppException e) {
      Log.e("Something is wrong", "Problem uploading");
    }

    return null;
  }

  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    dialog = new ProgressDialog(base);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("Uploading...");
        dialog.setIndeterminate(false);
        dialog.setCancelable(false);
        dialog.setMax(100);
        dialog.setProgress(0);
        dialog.show();
  }

  @Override
  protected void onPostExecute(Object result) {
    super.onPostExecute(result);
    dialog.dismiss();
  }

  @Override
  protected void onProgressUpdate(Integer... progress) {
    super.onProgressUpdate(progress);
    Log.e("progress", progress[0].toString());
    dialog.setProgress(progress[0]);
  }

}

这是输出Log.e("progress", progress[0].toString());

06-08 15:24:10.323: E/progress(13418): 21345
06-08 15:24:10.338: E/progress(13418): 10672
06-08 15:24:10.338: E/progress(13418): 7115
06-08 15:24:10.338: E/progress(13418): 5336
06-08 15:24:10.343: E/progress(13418): 4269
06-08 15:24:10.343: E/progress(13418): 3557
06-08 15:24:10.343: E/progress(13418): 3049
06-08 15:24:10.343: E/progress(13418): 2668
06-08 15:24:10.343: E/progress(13418): 2371
06-08 15:24:10.343: E/progress(13418): 2134
06-08 15:24:10.343: E/progress(13418): 1940
06-08 15:24:10.343: E/progress(13418): 1778
06-08 15:24:10.343: E/progress(13418): 1641
06-08 15:24:10.343: E/progress(13418): 1524
06-08 15:24:10.343: E/progress(13418): 1423
06-08 15:24:10.343: E/progress(13418): 1334
06-08 15:24:10.343: E/progress(13418): 1255
06-08 15:24:10.343: E/progress(13418): 1185
06-08 15:24:10.343: E/progress(13418): 1123
06-08 15:24:10.343: E/progress(13418): 1067
06-08 15:24:10.358: E/progress(13418): 1016
06-08 15:24:10.358: E/progress(13418): 970
06-08 15:24:10.358: E/progress(13418): 928
06-08 15:24:10.358: E/progress(13418): 889
06-08 15:24:10.358: E/progress(13418): 853
06-08 15:24:10.358: E/progress(13418): 820
06-08 15:24:10.358: E/progress(13418): 790
06-08 15:24:10.363: E/progress(13418): 762
06-08 15:24:10.363: E/progress(13418): 736
06-08 15:24:10.363: E/progress(13418): 711
06-08 15:24:10.363: E/progress(13418): 688
06-08 15:24:10.363: E/progress(13418): 667
06-08 15:24:10.363: E/progress(13418): 646
06-08 15:24:10.363: E/progress(13418): 627
06-08 15:24:10.363: E/progress(13418): 609
06-08 15:24:10.363: E/progress(13418): 592
06-08 15:24:10.363: E/progress(13418): 576
06-08 15:24:10.363: E/progress(13418): 561
06-08 15:24:10.363: E/progress(13418): 547
06-08 15:24:10.363: E/progress(13418): 533
06-08 15:24:10.363: E/progress(13418): 520
06-08 15:24:10.363: E/progress(13418): 508
06-08 15:24:10.363: E/progress(13418): 496
06-08 15:24:10.363: E/progress(13418): 485
06-08 15:24:10.363: E/progress(13418): 474
06-08 15:24:10.363: E/progress(13418): 464
06-08 15:24:10.363: E/progress(13418): 454
06-08 15:24:10.363: E/progress(13418): 444
06-08 15:24:10.363: E/progress(13418): 435
06-08 15:24:10.363: E/progress(13418): 426
06-08 15:24:10.363: E/progress(13418): 418
06-08 15:24:10.363: E/progress(13418): 410
06-08 15:24:10.363: E/progress(13418): 402
06-08 15:24:10.363: E/progress(13418): 395
06-08 15:24:10.363: E/progress(13418): 388
06-08 15:24:10.363: E/progress(13418): 381
06-08 15:24:10.373: E/progress(13418): 374
06-08 15:24:10.373: E/progress(13418): 368
06-08 15:24:10.403: E/progress(13418): 361
06-08 15:24:10.433: E/progress(13418): 355
06-08 15:24:10.433: E/progress(13418): 349
06-08 15:24:10.468: E/progress(13418): 344
06-08 15:24:10.473: E/progress(13418): 338
06-08 15:24:10.478: E/progress(13418): 333
06-08 15:24:10.478: E/progress(13418): 328
06-08 15:24:10.478: E/progress(13418): 323
06-08 15:24:10.478: E/progress(13418): 318
06-08 15:24:10.478: E/progress(13418): 313
06-08 15:24:10.478: E/progress(13418): 309
06-08 15:24:10.478: E/progress(13418): 304
06-08 15:24:10.478: E/progress(13418): 300
06-08 15:24:10.478: E/progress(13418): 296
06-08 15:24:10.478: E/progress(13418): 292
06-08 15:24:10.478: E/progress(13418): 288
06-08 15:24:10.478: E/progress(13418): 284
06-08 15:24:10.478: E/progress(13418): 280
06-08 15:24:10.478: E/progress(13418): 277
06-08 15:24:10.478: E/progress(13418): 273
06-08 15:24:10.478: E/progress(13418): 270
06-08 15:24:10.478: E/progress(13418): 266
06-08 15:24:10.478: E/progress(13418): 263
06-08 15:24:10.478: E/progress(13418): 260
06-08 15:24:10.478: E/progress(13418): 257
06-08 15:24:10.478: E/progress(13418): 254
06-08 15:24:10.478: E/progress(13418): 251
06-08 15:24:10.478: E/progress(13418): 248
06-08 15:24:10.478: E/progress(13418): 245
06-08 15:24:10.508: E/progress(13418): 242
06-08 15:24:10.508: E/progress(13418): 239
06-08 15:24:10.508: E/progress(13418): 237
06-08 15:24:10.508: E/progress(13418): 234
06-08 15:24:10.508: E/progress(13418): 232
06-08 15:24:10.508: E/progress(13418): 229
06-08 15:24:11.258: E/progress(13418): 227
06-08 15:24:11.278: E/progress(13418): 224
06-08 15:24:11.278: E/progress(13418): 222
06-08 15:24:11.278: E/progress(13418): 220
06-08 15:24:11.283: E/progress(13418): 217
06-08 15:24:11.313: E/progress(13418): 215
06-08 15:24:11.313: E/progress(13418): 213
06-08 15:24:11.318: E/progress(13418): 211
06-08 15:24:11.358: E/progress(13418): 209
06-08 15:24:11.378: E/progress(13418): 207
06-08 15:24:11.378: E/progress(13418): 205
06-08 15:24:11.378: E/progress(13418): 203
06-08 15:24:11.378: E/progress(13418): 201
06-08 15:24:11.378: E/progress(13418): 199
06-08 15:24:11.378: E/progress(13418): 197
06-08 15:24:11.378: E/progress(13418): 195
06-08 15:24:11.378: E/progress(13418): 194
06-08 15:24:11.378: E/progress(13418): 192
06-08 15:24:11.378: E/progress(13418): 190
06-08 15:24:11.378: E/progress(13418): 188
06-08 15:24:11.378: E/progress(13418): 187
06-08 15:24:11.378: E/progress(13418): 185
06-08 15:24:11.378: E/progress(13418): 184
06-08 15:24:11.378: E/progress(13418): 182
06-08 15:24:11.378: E/progress(13418): 180
06-08 15:24:11.378: E/progress(13418): 179
06-08 15:24:11.378: E/progress(13418): 177
06-08 15:24:11.378: E/progress(13418): 176
06-08 15:24:11.378: E/progress(13418): 174
06-08 15:24:11.378: E/progress(13418): 173
06-08 15:24:11.378: E/progress(13418): 172
06-08 15:24:11.378: E/progress(13418): 170
06-08 15:24:11.378: E/progress(13418): 169
06-08 15:24:11.378: E/progress(13418): 168
06-08 15:24:11.378: E/progress(13418): 166
06-08 15:24:11.418: E/progress(13418): 165
06-08 15:24:11.423: E/progress(13418): 164
06-08 15:24:11.423: E/progress(13418): 162
06-08 15:24:11.973: E/progress(13418): 161
06-08 15:24:12.003: E/progress(13418): 160
06-08 15:24:12.008: E/progress(13418): 159
06-08 15:24:12.008: E/progress(13418): 158
06-08 15:24:12.008: E/progress(13418): 156
06-08 15:24:12.008: E/progress(13418): 155
06-08 15:24:12.008: E/progress(13418): 154
06-08 15:24:12.008: E/progress(13418): 153
06-08 15:24:12.038: E/progress(13418): 152
06-08 15:24:12.038: E/progress(13418): 151
06-08 15:24:12.038: E/progress(13418): 150
06-08 15:24:12.038: E/progress(13418): 149
06-08 15:24:12.038: E/progress(13418): 148
06-08 15:24:12.038: E/progress(13418): 147
06-08 15:24:12.038: E/progress(13418): 146
06-08 15:24:12.038: E/progress(13418): 145
06-08 15:24:12.038: E/progress(13418): 144
06-08 15:24:12.038: E/progress(13418): 143
06-08 15:24:12.038: E/progress(13418): 142
06-08 15:24:12.038: E/progress(13418): 141
06-08 15:24:12.038: E/progress(13418): 140
06-08 15:24:12.038: E/progress(13418): 139
06-08 15:24:12.038: E/progress(13418): 138
06-08 15:24:12.038: E/progress(13418): 137
06-08 15:24:12.038: E/progress(13418): 136
06-08 15:24:12.038: E/progress(13418): 135
06-08 15:24:12.038: E/progress(13418): 135
06-08 15:24:12.038: E/progress(13418): 134
06-08 15:24:12.038: E/progress(13418): 133
06-08 15:24:12.058: E/progress(13418): 132
06-08 15:24:12.058: E/progress(13418): 131
06-08 15:24:12.058: E/progress(13418): 130
06-08 15:24:13.083: E/progress(13418): 130
06-08 15:24:13.113: E/progress(13418): 129
06-08 15:24:13.138: E/progress(13418): 128
06-08 15:24:13.138: E/progress(13418): 127
06-08 15:24:13.143: E/progress(13418): 127
06-08 15:24:13.143: E/progress(13418): 126
06-08 15:24:13.143: E/progress(13418): 125
06-08 15:24:13.143: E/progress(13418): 124
06-08 15:24:13.143: E/progress(13418): 124
06-08 15:24:13.143: E/progress(13418): 123
06-08 15:24:13.148: E/progress(13418): 122
06-08 15:24:13.148: E/progress(13418): 121
06-08 15:24:13.148: E/progress(13418): 121
06-08 15:24:13.148: E/progress(13418): 120
06-08 15:24:13.148: E/progress(13418): 119
06-08 15:24:13.148: E/progress(13418): 119
06-08 15:24:13.193: E/progress(13418): 118
06-08 15:24:13.193: E/progress(13418): 117
06-08 15:24:13.198: E/progress(13418): 117
06-08 15:24:13.198: E/progress(13418): 116
06-08 15:24:13.198: E/progress(13418): 116
06-08 15:24:13.198: E/progress(13418): 115
06-08 15:24:13.198: E/progress(13418): 114
06-08 15:24:13.198: E/progress(13418): 114
06-08 15:24:13.198: E/progress(13418): 113
06-08 15:24:13.198: E/progress(13418): 112
06-08 15:24:13.198: E/progress(13418): 112
06-08 15:24:13.198: E/progress(13418): 111
06-08 15:24:13.198: E/progress(13418): 111
06-08 15:24:13.198: E/progress(13418): 110
06-08 15:24:13.198: E/progress(13418): 110
06-08 15:24:13.198: E/progress(13418): 109
06-08 15:24:13.198: E/progress(13418): 108
06-08 15:24:13.218: E/progress(13418): 108
06-08 15:24:13.218: E/progress(13418): 107
06-08 15:24:13.218: E/progress(13418): 107
06-08 15:24:13.218: E/progress(13418): 106
06-08 15:24:13.223: E/progress(13418): 106
06-08 15:24:13.223: E/progress(13418): 105
06-08 15:24:13.223: E/progress(13418): 105
06-08 15:24:13.223: E/progress(13418): 104
06-08 15:24:13.223: E/progress(13418): 104
06-08 15:24:13.223: E/progress(13418): 103
06-08 15:24:13.223: E/progress(13418): 103
06-08 15:24:13.223: E/progress(13418): 102
06-08 15:24:13.223: E/progress(13418): 102
06-08 15:24:13.223: E/progress(13418): 101
06-08 15:24:13.223: E/progress(13418): 101
06-08 15:24:13.223: E/progress(13418): 100
06-08 15:24:13.603: E/progress(13418): 100
06-08 15:24:13.648: E/progress(13418): 100
4

1 回答 1

2
publishProgress((int) (total * 100 / transfer));

应该:

(int)(((float)transfer * 100f) / (float)total)
于 2013-06-08T14:37:43.510 回答