在我的应用程序中,当调用特定活动时,我正在显示来自网址的图像。由于来自url的图像将缓慢加载,我试图在此处显示进度对话框,
以下是显示进度对话框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任务时,
如何让进度从开始运行.....
答案 0 :(得分:4)
移动
Bitmap bm = getBitmapFromURL(Appconstants.image_url.get(Appconstants.img_i));
在
doInBackground
因为它是主要的漫长过程。你在onPostExecute
打电话。
onPostExecute
在主UI线程上运行。