2013-04-24 90 views
0

我有AsyncTask下載圖像,它也顯示進度對話框。 asynctask運行非常好,並且圖像下載的進度也正確顯示了值。我通過在doInBackground()中記錄下載的值來檢查它,然後使用publishProgress()。但是這個publishProgress不會增加bar進度對話框。Asynctask不更新進度對話框publishprogress

以下是我正在使用的代碼。

public class SingleMenuItemActivity extends Activity { 
public static final String KEY_TITLE = "title"; 
public static final String KEY_LINK = "link"; 
public static final String KEY_DATE = "date"; 

public static final String TAG = "SingleMenuItemActivity"; 
private ProgressDialog pDialog; 
// Progress dialog type (0 - for Horizontal progress bar) 
public static final int PROGRESS_BAR_TYPE = 0; 

//public static ImageDownloadTask imTask; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.single_list_item); 

    // getting intent data 
    Intent in = getIntent(); 

    // Get XML values from previous intent 
    String name = in.getStringExtra(KEY_TITLE); 
    String pLink = in.getStringExtra(KEY_LINK); 
    String pDate = in.getStringExtra(KEY_DATE); 

    // Displaying all values on the screen 
    TextView lblTitle = (TextView) findViewById(R.id.title_label); 
    TextView lblDate = (TextView) findViewById(R.id.publish_label); 
    lblTitle.setText(name); 
    lblDate.setText(pDate); 

// Set Image 
    try { 
     URL url = new URL(pLink); 
     new ImageDownloadTask().execute(url); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

/** 
* Showing Dialog 
* */ 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case PROGRESS_BAR_TYPE: // we set this to 0 
     pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Downloading image. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setMax(100); 
     pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
     return pDialog; 
    default: 
     return null; 
    } 
} 

class ImageDownloadTask extends AsyncTask<URL, Float, String> { 

    public static final String TAG = "ImageDownloadTask";   
    ImageView imageView = (ImageView) findViewById(R.id.single_list_imageview); 
    int count; 
    @Override 
    protected String doInBackground(URL... params) { 
     try { 

       URL url = params[0]; 
       URLConnection conection = url.openConnection(); 
       conection.connect(); 
       // this will be useful so that you can show a tipical 0-100% progress bar 
       int lenghtOfFile = conection.getContentLength(); 

       // download the file 
       InputStream input = new BufferedInputStream(url.openStream(), 8192); 

       // Output stream 
       OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg"); 

       byte data[] = new byte[1024]; 

       long total = 0; 

       while ((count = input.read(data)) != -1) { 
        total += count; 
        float pValue = (float)((total*100)/lenghtOfFile); 
        Log.d(TAG,"Download so far : "+pValue); 
        // publishing the progress.... 
        // After this onProgressUpdate will be called 
        publishProgress(pValue); 

        // writing data to file 
        output.write(data, 0, count); 
       } 

       // flushing output 
       output.flush(); 

       // closing streams 
       output.close(); 
       input.close(); 

      } catch (Exception e) { 
       Log.e("Error: ", e.getMessage()); 
      } 
     return null; 
    } 


    @Override 
    protected void onPostExecute(String result) { 

     Log.d(TAG,"Bitmap download complete"); 
     dismissDialog(PROGRESS_BAR_TYPE); 

     // Displaying downloaded image into image view 
     // Reading image path from sdcard 
     String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg"; 
     // setting downloaded into image view 
     imageView.setImageDrawable(Drawable.createFromPath(imagePath)); 
    } 

    @Override 
    protected void onCancelled(){ 
     Log.d(TAG,"Progress Dialog was Cancelled"); 
     pDialog.dismiss(); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Things to be done before execution of long running operation. For 
     // example showing ProgessDialog 
     showDialog(PROGRESS_BAR_TYPE); 
    } 

    protected void onProgressUpdate(Integer... values) { 
     //super.onProgressUpdate(values); 
     Log.d(TAG,"values"+values[0]); 
//   incrementProgressBy(values[0]); 
     //pDialog.setProgress(values[0]); 
     setProgress(values[0]); 
     } 
    } 
} 

請告訴我我在哪裏做錯了。

謝謝

+0

你應該在對話框本身上調用setProgress(),順便說一句,你確定你正在計算正確的進度? – Egor 2013-04-24 21:05:51

+0

是的,pValue按照需要進行計算。並示出爲在迄今爲止的logcat視圖 下載如下:99.0 下載到目前爲止:99.0 下載到目前爲止:100.0 我也曾嘗試已經 pDialog.setProgress(值[0]); 但是,進度對話框仍然不會增加。有什麼我可能會失蹤。 – Samundra 2013-04-24 21:09:36

回答

4

的事情是,你AsyncTask進度類型定義爲Float,而onProgressUpdate()方法需要Integer爲參數的類型。通過這種方式聲明onProgressUpdate(),您將超負載標準回調方法,並且不會被AsyncTask調用。如果您將@Override註釋添加到此方法,您的代碼也將無法編譯。因此,您應該將參數類型onProgressUpdate()更改爲Float,或者將進度類型AsyncTask更改爲Integer,這是更好的解決方案,因爲ProgressDialog'ssetProgress()需要將int作爲參數類型。

+0

謝謝,你的解決方案工作。我將參數更改爲Integer並添加了@Override註釋。 – Samundra 2013-04-24 21:25:19

+0

@Samundra,不客氣,很高興它的工作。 – Egor 2013-04-25 05:52:47