title: ImageView / Bitmap via AsyncTask Doesn't Show Properly
body: <p>Long time reader, first time poster. I'm very new to Android development and am having trouble getting images to show when using AsyncTask to insert ImageViews (containing Bitmaps) into a LinearLayout. This is all triggered in the onCreate() method of an Activity I have.</p>
  
  <p>The ImageViews (+Bitmaps) are definitely getting added via AsyncTask to my LinearLayout parent. However, the images don't show properly when I start my Activity. Sometimes an image or two (out of 3+) will display and sometimes none will show. All the images display properly after I fiddle with the UI, such as by bringing up and hiding the keyboard. I suspect that the LinearLayout and/or ImageViews may not be resizing to contain and show all the new children, but I tried many combinations of invalidate() and requestLayout() at the places I marked as "LOCATION1" and "LOCATION2" in attempt to trigger redraws.</p>
  
  <p>Would anyone help on ensuring all images are displayed properly after onCreate() and after each AsyncTask is complete? Thanks a bunch. I'll try to be succinct with my code snippets...</p>
  
  <p>This is my layout XML. I am adding my ImageViews to the LinearLayout with id "horizontal":</p>
  
  <pre><code>&lt;LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" &gt;
  
  &lt;EditText
      ... /&gt;
  
  &lt;HorizontalScrollView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" &gt;
  
      &lt;LinearLayout
          android:id="@+id/horizontal"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
      &lt;/LinearLayout&gt;
  &lt;/HorizontalScrollView&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is some of my onCreate() code. Where I create an AsyncTask for every image I want displayed.</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
  ...
  LinearLayout horizontal = (LinearLayout) findViewById(R.id.horizontal);
  ...
  //an array of absolute file paths to JPGs in storage
  ArrayList&lt;String&gt; images = report.getImageMain();
  PhotoBitmapTask task = null; //extension of AsyncTask
  for (int i = 0; i &lt; images.size(); i++) {
      task = new PhotoBitmapTask(getApplicationContext(), horizontal, images);
      task.execute(i);
      //LOCATION1
  }
  ...
  }
  </code></pre>
  
  <p>This is my extension of AsyncTask.</p>
  
  <pre><code>//a bunch of imports
  public class PhotoBitmapTask extends AsyncTask&lt;Integer, Void, Bitmap&gt; {
  
      private Context context;
      private WeakReference&lt;ViewGroup&gt; parent;
      private ArrayList&lt;String&gt; images;
      private int data;
  
      public PhotoBitmapTask(Context context, ViewGroup parent, ArrayList&lt;String&gt; images) {
          super();
  
          this.context = context;
          this.parent = new WeakReference&lt;ViewGroup&gt;(parent);
          this.images = images;
          this.data = 0;
      }
  
      @Override
      protected Bitmap doInBackground(Integer... params) {
          data = params[0];
          return getBitmapFromFile(images.get(params[0]), 600, 600);
      }
  
      @Override
      protected void onPostExecute(Bitmap result) {
          super.onPostExecute(result);
  
          if (context != null &amp;&amp; parent != null &amp;&amp; result != null) {
              ViewGroup viewGroup = parent.get();
              if (viewGroup != null) {
                  ImageView imageView = PhotoBitmapTask.getImageView(context);
                  imageView.setImageBitmap(result);
                  viewGroup.addView(imageView);
                  //LOCATION2
              }
          }
      }
  
      public static Bitmap getBitmapFromFile(String filePath, int maxHeight,
              int maxWidth) {
          // check dimensions for sample size
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(filePath, options);
  
          // calculate sample size
          options.inSampleSize = getSampleSize(options, maxHeight, maxWidth);
  
          // decode Bitmap with sample size
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(filePath, options);
      }
  
      public static int getSampleSize(BitmapFactory.Options options,
              int maxHeight, int maxWidth) {
          final int height = options.outHeight;
          final int width = options.outWidth;
          int sampleSize = 1;
  
          if (height &gt; maxHeight || width &gt; maxWidth) {
              // calculate ratios of given height/width to max height/width
              final int heightRatio = Math.round((float) height / (float) maxHeight);
              final int widthRatio = Math.round((float) width / (float) maxWidth);
  
              // select smallest ratio as the sample size
              if (heightRatio &gt; widthRatio)
                  return heightRatio;
              else
                  return widthRatio;
          } else
              return sampleSize;
      }
  
      public int getData() {
          return this.data;
      }
  
      public static ImageView getImageView(Context context) {
          // width and height
          final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.WRAP_CONTENT,
                  LinearLayout.LayoutParams.WRAP_CONTENT);
          // margins
          params.setMargins(20, 20, 20, 20);
          final ImageView view = new ImageView(context);
          view.setLayoutParams(params);
          // scale type
          view.setScaleType(ScaleType.CENTER);
          return view;
      }
  }
  </code></pre>