文件已成功上传,但Android上传进度条始终为0%

时间:2016-09-06 03:19:34

标签: android file-upload progressdialog android-progressbar

我创建了一个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%而不是提升。

它看起来像这样:

Upload Progress

2 个答案:

答案 0 :(得分:1)

我建议延长AsyncTask。有在线教程,例如http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/,它将帮助您上传文件

答案 1 :(得分:0)

您不应该操纵来自非ui线程的视图。使用Handler更新进度。

编辑:

有关完整示例,请参阅this