title: Android Display Images from directory
body: <p>I am trying to display images from a specified directory on an android device in a gallery type activity. i was following (at least trying to) the guide on displaying images on the android developers site but kept getting lost  <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html" rel="nofollow">http://developer.android.com/training/displaying-bitmaps/load-bitmap.html</a> . I currently have a downsampled version of the image and am loading it into an image view in the layout.xml file. but this would require me to know the amount of images that will be present which i wont and i fear it is wasteful. </p>
  
  <p>I need to Know how to create the image views depending on how many images are in the directory. and display the thumbnails or downsampled images in the created imageViews.</p>
  
  <p><strong>layout.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" 
         tools:context=".MainActivity"&gt;
      &lt;TextView android:text="Hello World!" android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/textView" /&gt;
  
      &lt;ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/mImageView0"
         android:layout_centerHorizontal="true"
         android:layout_alignParentEnd="true" /&gt;
      ............
      &lt;ImageView
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/mImageView5"
          android:layout_below="@id/mImageView4"
          android:layout_centerHorizontal="true"
          android:layout_alignParentEnd="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private final String TAG = "MainActivity Class";
  
      public ImageView mImageView;
      public ListView listView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          String fileDir = Environment.getExternalStorageDirectory().toString()+ File.separator +"Camera-App-Images"+ File.separator;
          File f = new File(fileDir);
          Log.d(TAG, fileDir);
          File file[] = f.listFiles();
  
          ImageView[] imgViews = new ImageView[file.length];
  
          for (int i=0; i &lt; file.length; i++) {
              mImageView =  (ImageView)findViewById(R.id.mImageView0 + i);
              listView = (ListView)findViewById(R.id.listView);
              loadBitmap(fileDir + file[i].getName(), mImageView);
          }
      }
  
      public void loadBitmap(String fileLoc, ImageView imageView) {
          BitmapWorkerTask task = new BitmapWorkerTask(fileLoc ,imageView);
          task.execute();
      }
  }
  </code></pre>
  
  <p><strong>BitmapWorkerTask</strong></p>
  
  <pre><code>class BitmapWorkerTask extends AsyncTask&lt;Integer, Void, Bitmap&gt; {
      private final WeakReference&lt;ImageView&gt; imageViewReference;
      private int data = 0;
  
      private String imagePath = "";
  
      private final String TAG = "BitmapWorkerTask";
  
      public BitmapWorkerTask(String imgPath, ImageView imageView) {
          imagePath = imgPath;
          // Use a WeakReference to ensure the ImageView can be garbage collected
          imageViewReference = new WeakReference&lt;ImageView&gt;(imageView);
  
          Log.d(TAG,"Constructor");
      }
  
      // Decode image in background.
      @Override
      protected Bitmap doInBackground(Integer... params) {
           //data = params[0];
           return decodeSampledBitmapFromFile(imagePath, 100, 100);
      }
  
      // Once complete, see if ImageView is still around and set bitmap.
      @Override
      protected void onPostExecute(Bitmap bitmap) {
          if (imageViewReference != null &amp;&amp; bitmap != null) {
               final ImageView imageView = imageViewReference.get();
               if (imageView != null) {
                  imageView.setImageBitmap(bitmap);
  
              }
          }
      }
  
      public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {
  
          // First decode with inJustDecodeBounds=true to check dimensions
          final BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(filePath, options);
  
          // Calculate inSampleSize
          options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  
          // Decode bitmap with inSampleSize set
          options.inJustDecodeBounds = false;
          return BitmapFactory.decodeFile(filePath, options);
      }
  
      public static int calculateInSampleSize(
              BitmapFactory.Options options, int reqWidth, int reqHeight) {
          // Raw height and width of image
          final int height = options.outHeight;
          final int width = options.outWidth;
          int inSampleSize = 1;
  
           if (height &gt; reqHeight || width &gt; reqWidth) {
  
              final int halfHeight = height / 2;
              final int halfWidth = width / 2;
  
              while ((halfHeight / inSampleSize) &gt; reqHeight
                      &amp;&amp; (halfWidth / inSampleSize) &gt; reqWidth) {
                  inSampleSize *= 2;
              }
          }
          return inSampleSize;
      }
  }
  </code></pre>