0

Well I am downloading a image from url with AsyncTask, I want to show this image in dialog, right after image downloaded. I m trying with this code: It ıs not waiting until finish, this code is returning RUNNING.

if(dt.getStatus() == AsyncTask.Status.FINISHED) {

        dialog.show();

    }
mehmet
  • 1,558
  • 5
  • 30
  • 41

2 Answers2

0

You need to set an event listener. Just setting an if-then statement means it will be executed the moment it finds the code. A event listener will wait until the event happens (In this case, Status.FINISHED) and then execute the code inside the listener.

JRad the Bad
  • 511
  • 5
  • 25
  • Here's a great example of how to set your own custom event listener: http://stackoverflow.com/questions/8292712/android-custom-event-listener – JRad the Bad Mar 17 '14 at 20:59
0

With Ken Wolf helps..

        public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        private Context mcontec;

        public DownloadImageTask(ImageView bmImage, Context mContextr) {
            this.bmImage = bmImage;
            mcontec=mContextr;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {

            dialog(result, mcontec);
        }

        public void dialog(Bitmap resim, final Context ctx){

            final Dialog dialog = new Dialog(ctx, R.style.CustomDialogTheme);

            dialog.setContentView(R.layout.perdereklam);
            ImageView reklam = (ImageView) dialog.findViewById(R.id.reklampng);
            reklam.setImageBitmap(resim);
            reklam.setOnTouchListener(new OnTouchListener() {

            // fill your stuff...

    }

        dialog.show();
}

calling from any class:

public static void showRateDialog(final Context mContextr) {
    final Dialog dialog = new Dialog(mContextr, R.style.CustomDialogTheme);
    ImageView reklam = (ImageView) dialog.findViewById(R.id.reklampng);
    final DownloadImageTask dt = new DownloadImageTask(reklam, mContextr);
            dt.execute("your url");
}
mehmet
  • 1,558
  • 5
  • 30
  • 41