title: Android Studio Display Image in Alert Dialog
body: <p>I have an image store in the firebase realtime db as a url. I am trying to download the image and display the image in an alert dialog message. When excuting the code the alert dialog appears but no image is displayed.</p>
  
  <p><a href="https://i.stack.imgur.com/Nsbda.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nsbda.png" alt="enter image description here"></a></p>
  
  <p>ImageDownloader Class:</p>
  
  <pre><code>import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.AsyncTask;
  import android.widget.ImageView;
  
  import java.io.InputStream;
  import java.lang.ref.WeakReference;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  
  public class ImageDownload extends AsyncTask&lt;String, Void, Bitmap&gt;
  
  {
      private final WeakReference&lt;ImageView&gt; imageViewWeakReference;
  
      public ImageDownload(ImageView imageView)
      {
          imageViewWeakReference = new WeakReference&lt;&gt;(imageView);
      }
  
      @Override
      protected Bitmap doInBackground(String... string)
      {
          return downloadBitmap(string[0]);
      }
  
      @Override
      protected void onPostExecute(Bitmap bitmap)
      {
          if(isCancelled())
          {
              bitmap = null ;
          }
  
          ImageView imageView = imageViewWeakReference.get();
          if (imageView != null)
          {
              if (bitmap != null)
              {
                  imageView.setImageBitmap(bitmap);
              }
          }
      }
  
      private Bitmap downloadBitmap(String url)
      {
          HttpURLConnection connection = null;
  
          try
          {
              URL url1 = new URL(url);
              connection = (HttpURLConnection) url1.openConnection();
              int StatusCode = connection.getResponseCode();
  
              if(StatusCode != connection.HTTP_OK)
              {
                  return null;
              }
  
              InputStream inputStream = connection.getInputStream();
              if(inputStream != null)
              {
                  return BitmapFactory.decodeStream(inputStream);
  
              }
          }
          catch (Exception e)
          {
              connection.disconnect();
          }
          finally
          {
              if(connection != null)
              {
                  connection.disconnect();
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>I created a layout xml file for the alert dialog:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
  
      &lt;ImageView
          android:id="@+id/dialog_imageview"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <pre><code>  btVoucher1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  databaseReference.addValueEventListener(new ValueEventListener() {
  
                      @Override
                      public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                      {
                          String qrcode = dataSnapshot.child("Voucher").child("1").getValue().toString();
  
                          Toast.makeText(getActivity(), qrcode, Toast.LENGTH_LONG).show();
                          AlertDialog.Builder alertVoucher1 = new AlertDialog.Builder(getContext());
                          alertVoucher1.setNeutralButton("Close",
                                  new DialogInterface.OnClickListener()
                                  {
                                      public void onClick(DialogInterface dialog, int id)
                                      {
                                          dialog.cancel();
  
                                      }
                                  });
  
                          new ImageDownload(qrCodeVoucher).execute(qrcode);
                          AlertDialog voucher = alertVoucher1.create();
                          voucher.show();
  
                      }
  
                      @Override
                      public void onCancelled(@NonNull DatabaseError databaseError)
                      {
                          System.out.println("The read failed: " + databaseError.getCode());
                      }
                  });
              }
          });
  </code></pre>
  
  <p>Can anyone provide any help? Thank you.</p>