Android ProgressDialog显示但进度未更新

时间:2013-06-07 09:02:56

标签: android user-interface android-asynctask progressdialog

我正在尝试在创建后更新进度条(都在AsyncTask中)。进度对话框显示并解除但未更新,它始终设置为0.这是我的代码:     public class ImageTask扩展了AsyncTask     {

    @Override
    protected Void doInBackground(String... saleId) {

        //Drawable drawable = getActivity().getResources().getDrawable(R.drawable.sale_image_holder);
        //Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        //ByteArrayOutputStream stream = new ByteArrayOutputStream();
        //bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        //byte[] imageInAsByteArray = stream.toByteArray(); // here
        ImgService imgService = new ImgService();
        imgService.addImgToSale(imageInAsByteArray, saleId[0], this);
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) 
    {
        super.onProgressUpdate(progress);
        dialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
    }

    public void doProgress(int value){
        publishProgress(value);
    }
}

这是ImgService类:

public class ImgService 
{
    public void addImgToSale(byte[] img, final String saleId, final ImageTask task)
    {
        final ParseFile file = new ParseFile(saleId + ".jpeg", img);
        try {
            file.save();
        } catch (ParseException e1) {
            e1.printStackTrace();
            return;
        }
        task.doProgress(50);
        ParseObject image = new ParseObject(DBDictionary.SALE_IMAGES);
        ParseObject sale = new ParseObject(DBDictionary.SALE);
        sale.setObjectId(saleId);
        image.put(DBDictionary.SALE_IMAGES_SALE_ID_P, ParseObject.createWithoutData(DBDictionary.SALE, saleId));
        image.put("image", file);
        try {
            image.save();
        } catch (ParseException e1) {
            e1.printStackTrace();
            return;
        }
        task.doProgress(100);
    }
}

在网上,我发现显示和解雇ProgressDialog有很多问题,但没有更新进度。

谢谢!

4 个答案:

答案 0 :(得分:4)

从android doc:在asynctask中使用onProgressUpdate函数。

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
  protected Long doInBackground(URL... urls) {
     int count = urls.length;
     long totalSize = 0;
     for (int i = 0; i < count; i++) {
         totalSize += Downloader.downloadFile(urls[i]);
         publishProgress((int) ((i / (float) count) * 100));//this function updates the progress bar ...
         // Escape early if cancel() is called
         if (isCancelled()) break;
     }
     return totalSize;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
 }
}

答案 1 :(得分:2)

请参阅以下代码

class DownloadFileAsync extends AsyncTask<String, String, String> {   

 @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Downloading file..");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    @Override
    protected String doInBackground(String... aurl) {
        int count;

    try {

    URL url = new URL(aurl[0]);
    URLConnection conexion = url.openConnection();
    conexion.connect();

    int lenghtOfFile = conexion.getContentLength();
    Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
    FileOutputStream obj;
    InputStream input = new BufferedInputStream(url.openStream());
    String Path="/sdcard/";

    OutputStream output = new FileOutputStream(Path+"some_photo_from_gdansk_poland.jpg");

    byte data[] = new byte[1024];

    long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {}
    return null;

    }
    protected void onProgressUpdate(String... progress) {
         Log.d("ANDRO_ASYNC",progress[0]);
         mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

}

添加清单 android.permission.WRITE_EXTERNAL_STORAGE

答案 2 :(得分:1)

从postexecute方法中删除dialog.setProgress(50)行,并将以下代码用于asynctask。

protected void onProgressUpdate(Integer... values) 
                    {
                       dialog.setProgress(values[0]);    
                    }

答案 3 :(得分:0)

将“不确定”设置为false对我有用:

dialog.setIndeterminate(false);

https://developer.android.com/reference/android/widget/ProgressBar.html#setIndeterminate(boolean)