【问题标题】:android - progress dialog not appearing soonandroid - 进度对话框不会很快出现
【发布时间】:2012-10-17 02:32:07
【问题描述】:

在我的应用程序中,当调用特定活动时,我正在显示来自 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;
    }

}

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

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

【问题讨论】:

    标签: android progressdialog


    【解决方案1】:

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

    doInBackground

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

    onPostExecute 在主 UI 线程上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多