进度和进度的ProgressDialog

时间:2015-07-22 13:37:14

标签: android progressdialog


我在我的应用程序中使用AsyncTask下载了几张图片。这个asynctask是在一个单独的类中实现的,我使用一个接口来更新UI线程。执行任务时会显示progressDialog并更新进度。
到目前为止,我可以显示当前的下载进度,或已经下载的图片数量,我想同时做两个。
这是我的asynctask类:

public class DownloadPicturesTask extends AsyncTask<String ,Integer , Boolean> {
/**
 * Interface updating the UI thread thanks to its methods
 */
public DownloadPicturesResponse handler = null;
private Activity callingActivity = null;
private String message;

public DownloadPicturesTask(Activity activity){
    attach(activity);
}

public void attach(Activity activity){
    this.callingActivity = activity;
}

public void detach(){
    this.callingActivity = null;
}

@Override
protected void onPreExecute() {
    handler.picturesPreExecute();
}

/**
 * For each picture, we store the downloaded file in the historic folder <br/>
 * Ff all pictures are successfully downloaded, return true <br/>
 * If fail or cancel return false.
 * @param urls list of picture names
 * @return boolean
 */
@Override
protected Boolean doInBackground(String... urls) {
    InputStream input;
    OutputStream output;
    HttpURLConnection connection;
    try{
        for (int i = 0 ; i < urls.length ;i++ ) {
            String url = urls[i];
            URL newURL = new URL(Config.URL_PICTURES + url);
            connection = (HttpURLConnection) newURL.openConnection();
            connection.connect();
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                message = connection.getResponseCode() + " : " + connection.getResponseMessage();
                return Boolean.FALSE;
            }
            int fileLength = connection.getContentLength();
            input = connection.getInputStream();
            File historicFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Config.HISTORIC_DIRECTORY);
            if (!historicFolder.exists()) {
                boolean mkdir = historicFolder.mkdir();
                if (!mkdir) {
                    message = "Impossible de créer le dossier historique.";
                    return Boolean.FALSE;
                }
            }
            output = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + Config.HISTORIC_DIRECTORY + "/" + url);
            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                if (isCancelled()) {
                    input.close();
                    message = "Téléchargement annulé";
                    return Boolean.FALSE;
                }
                total += count;
                /* here is where i publish progress  */
                if (fileLength > 0)
                    publishProgress((int) (total * 100 / fileLength),i);
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        }
    }catch (Exception e){
        e.printStackTrace();
        Log.e(Config.LOG_ERROR, "MeasurePictureTask", e);
        message = e.toString();
        return Boolean.FALSE;
    }
    message = "Téléchargement terminé";
    return Boolean.TRUE;
}

/**
 * Calling the handler concerned method
 * @param values integer value of the progress
 */
@Override
protected void onProgressUpdate(Integer... values) {
    handler.picturesOnProgressUpdate(values[0],values[1]);
}

/**
 * Calling the handler concerned method
 * @param succeed boolean
 */
@Override
protected void onPostExecute(Boolean succeed) {
    if(callingActivity != null)
        handler.picturesOnPostExecute(succeed,message);
    else {
        Log.e(Config.LOG_ERROR,"DownloadPicture Task : Calling Activity has been lost.");
    }
}

这里是调用活动(只有asyncTask部分):

@Override
public void picturesPreExecute() {
    pDial = new ProgressDialog(this);
    pDial.setMessage("Chargement des photos");
    pDial.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pDial.setMax(pictures.size());
    pDial.setCancelable(true);
    pDial.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            pictureTask.cancel(true);
        }
    });
    pDial.show();
}

@Override
public void picturesOnProgressUpdate(int progress,int nbPictures) {
    pDial.setSecondaryProgress(progress);
    pDial.setProgress(nbPictures);
}

@Override
public void picturesOnPostExecute(boolean succeed, String result) {
    pDial.dismiss();
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    if(succeed)
        Toast.makeText(this,R.string.t_picture_download_succeed,Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
}

我想要这种显示,具有实际下载的百分比值,以及数值已全部下载的图片: enter image description here

2 个答案:

答案 0 :(得分:1)

为此你需要创建扩展progessBar的新类。有关详细信息,请查看http://colintmiller.com/how-to-add-text-over-a-progress-bar-on-android/

答案 1 :(得分:1)

你应该创建一个布局,其中包含下载百分比的进度条和每个图像增加的TextView。然后膨胀片段的布局