我创建了一个ProgressDialog,比如变量名"对话框"。
ProgressDialog dialog;
这是我的ProgressDialog代码:
//on upload button Click
if(selectedFilePath != null){
// dialog = ProgressDialog.show(MainActivity.this,"","Uploading File...",true);
dialog = new ProgressDialog(this);
dialog.setMessage("Uploading File");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setIndeterminate(true);
dialog.setMax(100);
dialog.show();
final int totalProgressTime = 100;
new Thread(new Runnable() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
Thread.sleep(2000);
jumpTime += 10;
dialog.setProgress(jumpTime);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//creating new thread to handle Http Operations
uploadFile(selectedFilePath);
}
}).start();
}else{
Toast.makeText(MainActivity.this,"Please choose a File First",Toast.LENGTH_SHORT).show();
}
我的文件成功上传。但是我的进度条上出了问题,它总是0%而不是提升。
它看起来像这样:
答案 0 :(得分:1)
我建议延长AsyncTask。有在线教程,例如http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/,它将帮助您上传文件
答案 1 :(得分:0)