3

我从过去 2 天开始一​​直坚持.. 在我的情况下,onProgressUpdate() 没有被调用.. 所以它没有更新进度条.. 任何人都可以看看并提出建议.. 谢谢。这是我的代码

package com.example.downloadupload;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.ProgressListener;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.exception.DropboxException;

public class DownloadFile extends AsyncTask<Void, Long, Boolean> {

    DropboxAPI<AndroidAuthSession> dDBApi;
    Context dContext;
    private final  ProgressDialog uDialog;
    private long dFileLen;
    long bytess;

    public DownloadFile(Context context,
        DropboxAPI<AndroidAuthSession> mDBApi) {
        dDBApi=mDBApi;
        dContext=context.getApplicationContext();
        uDialog = new ProgressDialog(context);
        uDialog.setMax(100);
        uDialog.setMessage("Downloading Image");
        uDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        uDialog.show();
    }

@Override
protected Boolean doInBackground(Void... params) {

    String path1= Environment.getExternalStorageDirectory()+"/log.txt";
    BufferedOutputStream out=null;
    try {
        File file = new File(path1);
        out = new BufferedOutputStream(new FileOutputStream(file));
        dDBApi.getFile("/log.txt", null,out,new ProgressListener() {

          /* @Override
           public long progressInterval() {
               // Update the progress bar every half-second or so
               return 500;
           }*/

            @Override
            public void onProgress(long bytes, long total) {
                // TODO Auto-generated method stub
                bytess=bytes;
                publishProgress(bytes);
            }
        });
    } catch (DropboxException e) {
        Log.e("DbExampleLog", "Something went wrong while downloading.");
    } catch (FileNotFoundException e) {
        Log.e("DbExampleLog", "File not found.");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {}
        }
    }
    return null;
}

@Override
protected void onProgressUpdate(Long... progress) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(progress);
    int percent = (int)(100.0*(double)progress[0]/bytess + 0.5);
    uDialog.setProgress(percent);
    System.out.println("Hi progressing");
}

@Override
protected void onPostExecute(Boolean result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    uDialog.dismiss();
    System.out.println("calling post execute");
}


}
4

3 回答 3

3

而不是这个

int percent = (int)(100.0*(double)progress[0]/bytess + 0.5);

用这个

int percent = (int)(100.0*(double)progress[0]/TotalsizeOfFile  + 0.5);

你可以取消注释

       @Override

       public long progressInterval() 

       {

           // Update the progress bar every half-second or so

           return 500;

       }

它对我有用。

于 2013-08-06T05:49:54.920 回答
0

我认为错误是这个

bytess=bytes;
publishProgress(bytes);    

接着,

int percent = (int)(100.0*(double)progress[0]/bytess + 0.5); 

在这里,progress[0] = bytess -> everytime,因此进度可能不会得到更新。

于 2012-09-25T10:32:33.587 回答
0

使 onProgress() 像这样

public void onProgress(long bytes, long total) {
// TODO Auto-generated method stub
    long[2] bytess = {bytes, total};
    publishProgress(bytess);
 }

并计算百分比

int percent = (int)(100.0*(double)progress[0]/progress[1]+ 0.5); 
于 2012-09-25T10:46:06.153 回答