文件上传成功,但 Android 上的上传进度条一直为 0%
File uploaded successfully, but upload progress bar on Android always 0%
我创建了一个类似变量名 "dialog" 的 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%,而且没有提升。
看起来像这样:
您不应从非ui 线程操作视图。使用 Handler
更新进度。
编辑:
有关完整示例,请参阅 this。
我建议扩展 AsyncTask. There are tutorial online, such as http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ 这将有助于您上传文件
我创建了一个类似变量名 "dialog" 的 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%,而且没有提升。
看起来像这样:
您不应从非ui 线程操作视图。使用 Handler
更新进度。
编辑:
有关完整示例,请参阅 this。
我建议扩展 AsyncTask. There are tutorial online, such as http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ 这将有助于您上传文件