in my app when a particular activity is been called i am showing a image from an url. As the image from url will gets loaded slowly, i am trying to show a progress dialog here,
following is my code to show progress dialog beofr image appears
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;
}
}
The progress dialog does not appear immediately, it takes to appear for micro second before the image gets appeared, but the logs in pre-execute and do in background gets printed immediately. when the asyn task been called,
how to make the progress to run from the begin…..
move
Bitmap bm = getBitmapFromURL(Appconstants.image_url.get(Appconstants.img_i));in
doInBackgroundAs it is the main long process. Which you are calling in
onPostExecute.onPostExecuterun on Main UI thread.