2012-10-16 38 views
0

在我的應用程序中,當某個活動被調用時,我正在顯示來自url的圖像。從URL中的圖像會被慢慢地加載,我想顯示一個進度對話框這裏,android-進度對話框不會很快出現

下面是我的代碼,以顯示進度對話框beofr形象出現

class ShowImageTagList extends AsyncTask<Void, Void, Void> 
{ 
    ProgressDialog dialog = new ProgressDialog(UploadPhoto.this); 
    protected void onPreExecute() 
    { 
      Log.e("preexcute ","called"); 
      this.dialog.setMessage(" Loading ..."); 
      this.dialog.setCancelable(false); 
      this.dialog.show(); 
    } 

    protected Void doInBackground(Void... args) 
    { 
     try 
     { 
      JSONObject json = new JSONObject(Appconstants.photo_details); 
      JSONArray photoperson = json.getJSONArray("photopersons"); 
      Log.e("photoperson ","value @ photoperson "+photoperson); 
      for(int j=0; j < photoperson.length(); j++) 
      { 
       id.add(photoperson.getJSONObject(j).getString("pid").toString()); 
       names.add(photoperson.getJSONObject(j).getString("name").toString()); 
      } 
     } 
     catch(Exception e) 
     { 
      Log.e("Eception caught", ""+e); 
     } 
     return null ;  
     } 

     protected void onPostExecute(Void unused) 
     { 
      Log.e("post execute ","called"); 
      Bitmap bm = getBitmapFromURL(Appconstants.image_url.get(Appconstants.img_i)); 
      img_to_upload.setImageBitmap(bm); 
      list_tag.setAdapter(new ListviewAdapter(UploadPhoto.this, names, id)); 
      dialog.dismiss(); 
     } 
} 

public static Bitmap getBitmapFromURL(String src) 
{ 
    try 
    { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
     Log.e("Exception",e.getMessage()); 
     return null; 
    } 

}

進度對話框不會立即出現,它會在圖像出現之前以微秒出現,但預先執行的日誌和後臺執行的日誌會立即打印出來。當ASYN任務被調用,

如何使從開始運行進度.....

回答

4

移動 Bitmap bm = getBitmapFromURL(Appconstants.image_url.get(Appconstants.img_i));

doInBackground

,因爲它是主要的長處理。你在onPostExecute打電話給你。

onPostExecute在主UI線程上運行。