0

在我的应用程序中,当调用特定活动时,我正在显示来自 url 的图像。由于来自 url 的图像加载缓慢,我试图在这里显示一个进度对话框,

以下是我显示进度对话框的代码,然后出现图像

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;
    }

}

进度对话框不会立即出现,它会在图像出现之前出现微秒,但会立即打印预执行和后台执行的日志。当异步任务被调用时,

如何让进度从一开始就运行......

4

1 回答 1

4

搬进 Bitmap bm = getBitmapFromURL(Appconstants.image_url.get(Appconstants.img_i));

doInBackground

因为它是主要的漫长过程。你打电话给哪个onPostExecute

onPostExecute在主 UI 线程上运行。

于 2012-10-16T18:25:31.077 回答