我嘗試將文件加載到服務器。我需要等待所有操作完成之後才能開始使用其他方法。所以我使用同步呼叫。我嘗試在啓動新線程之前顯示ProgressDialog,但是,當所有線程都沒有完成時,我的UI線程就卡住了。ProgressDialog在新線程之前不啓動線程
private void uploadImageSync() {
final ProgressDialog progressDialog;
progressDialog = new ProgressDialog(BarcodActivity.this);
progressDialog.setMessage("Load...");
progressDialog.show();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface service = retrofit.create(RequestInterface.class);
int i=0;
while (i++ <= 4) {
File f = getOutputMediaFilePath(mCode + "_"+i, true);
if(f.exists()){
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f);
MultipartBody.Part body = MultipartBody.Part.createFormData("file", f.getName(), requestFile);
final Call<ResponseBody> resultCall = service.uploadImage(body);
Thread t = new Thread(new Runnable() {
public void run() {
try {
resultCall.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
}});
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
progressDialog.dismiss();
}