title: Android: Get multiple images from Gallery and Populate them in a list view in android
body: <p>I want to get multiple images from Gallery and populate those images in ListView. First of all, is it possible to do it without using third party libraries?</p>
  
  <p>This is what I am doing currently....
  I am using universal_image_loader to get multiple images from Gallery, but I am not able to populate them in ListView.</p>
  
  <p><strong>"main.xml"</strong> - imageList is the List containing images from Gallery</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"
  android:orientation="vertical" &gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0.85"
      android:orientation="horizontal" &gt;
  
      &lt;ImageView
          android:id="@+id/image1"
          android:layout_width="100dp"
          android:layout_height="match_parent"
          android:layout_gravity="left"
          android:layout_weight="1" /&gt;
  
      &lt;ListView
          android:id="@+id/imageList"
          android:layout_width="70dp"
          android:layout_height="match_parent"
          android:layout_gravity="right"
          android:layout_weight="1" &gt;
  
  &lt;/ListView&gt;
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" &gt;
  
      &lt;Button
          android:id="@+id/button_photo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:onClick="getPhoto"
          android:text="Gallery" /&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>"list_row.xml"</strong> - view of single row of List</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"
  android:orientation="vertical" &gt;
  
  &lt;ImageView
          android:id="@+id/imageThumb"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:src="@drawable/index"/&gt;
  
  &lt;TextView 
      android:id="@+id/listText"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>"MainActivity.java"</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
  Button buttonPhoto;
  ImageView image1;
  ListView imageList;
  ListAdapter adapter;
  ArrayList&lt;String&gt; selectedPhotosList = null;
  String[] selectedPhotos = null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      buttonPhoto = (Button) findViewById(R.id.button_photo);
      imageList = (ListView) findViewById(R.id.imageList);
  
      adapter = new ListAdapter(this, selectedPhotosList);
      imageList.setAdapter(adapter);
  
  }
  
  public void getPhoto(View view) {
      Intent i = new Intent(this, MultiPhotoSelectActivity.class);
      startActivityForResult(i, 0);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
      super.onActivityResult(requestCode, resultCode, data);
      if(requestCode == 0) {
          selectedPhotosList = data.getStringArrayListExtra("selectedPhotos");
  
          //refresh list
          adapter.notifyDataSetChanged();
          Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Photos: " + selectedPhotos);
      }
  }
  
  }
  </code></pre>
  
  <p><strong>"ListAdapter.java"</strong> - adapter for the List populating images from Gallery</p>
  
  <pre><code>public class ListAdapter extends BaseAdapter {
  
  private static LayoutInflater inflater=null;
  private Activity activity;
  private ArrayList&lt;String&gt; imageData;
  
  public ListAdapter(Activity a, ArrayList&lt;String&gt; list) {
      activity = a;
      imageData = list;
      inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
  
  @Override
  public int getCount() {
      // TODO Auto-generated method stub
      return imageData.size();
  }
  
  @Override
  public Object getItem(int position) {
      // TODO Auto-generated method stub
      return position;
  }
  
  @Override
  public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View vi = convertView;
  
      if(convertView==null)
          vi = inflater.inflate(R.layout.list_row, null);
  
      ImageView thumb_image=(ImageView)vi.findViewById(R.id.imageThumb); // thumb image
      TextView text=(TextView)vi.findViewById(R.id.listText);
  
      Bitmap myBitmap = BitmapFactory.decodeFile(imageData.get(position));
      thumb_image.setImageBitmap(myBitmap);
      text.setText(position);
  
      return vi;
  }
  
  }
  </code></pre>
  
  <p>When I comes back to the MainActivity, I don't see any images in the ListView. Please help me out. What am I doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>"MultiPhotoSelectActivity.java</strong> - returns the path of all the images selected</p>
  
  <pre><code>package com.example.demo1;
  
  import java.util.ArrayList;
  
  import android.content.Context;
  import android.database.Cursor;
  import android.graphics.Bitmap;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.util.Log;
  import android.util.SparseBooleanArray;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.animation.Animation;
  import android.view.animation.AnimationUtils;
  import android.widget.BaseAdapter;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.Toast;
  import android.widget.CompoundButton.OnCheckedChangeListener;
  import android.widget.GridView;
  import android.widget.ImageView;
  
  import com.nostra13.universalimageloader.core.DisplayImageOptions;
  import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
  
  
  public class MultiPhotoSelectActivity extends BaseActivity {
  
  private ArrayList&lt;String&gt; imageUrls;
  private DisplayImageOptions options;
  private ImageAdapter imageAdapter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.ac_image_grid);
  
      final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
      final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
      Cursor imagecursor = managedQuery(
              MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
              null, orderBy + " DESC");
  
      this.imageUrls = new ArrayList&lt;String&gt;();
  
      for (int i = 0; i &lt; imagecursor.getCount(); i++) {
          imagecursor.moveToPosition(i);
          int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
          imageUrls.add(imagecursor.getString(dataColumnIndex));
  
          System.out.println("=====&gt; Array path =&gt; "+imageUrls.get(i));
      }
  
      options = new DisplayImageOptions.Builder()
          .showStubImage(R.drawable.stub_image)
          .showImageForEmptyUri(R.drawable.image_for_empty_url)
          .cacheInMemory()
          .cacheOnDisc()
          .build();
  
      imageAdapter = new ImageAdapter(this, imageUrls);
  
      GridView gridView = (GridView) findViewById(R.id.gridview);
      gridView.setAdapter(imageAdapter);
      /*gridView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              startImageGalleryActivity(position);
          }
      });*/
  }
  
  @Override
  protected void onStop() {
      imageLoader.stop();
      super.onStop();
  }
  
  public void btnChoosePhotosClick(View v){
  
      ArrayList&lt;String&gt; selectedItems = imageAdapter.getCheckedItems();
      Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: "+selectedItems.size(), Toast.LENGTH_SHORT).show();
      Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
  }
  
  /*private void startImageGalleryActivity(int position) {
      Intent intent = new Intent(this, ImagePagerActivity.class);
      intent.putExtra(Extra.IMAGES, imageUrls);
      intent.putExtra(Extra.IMAGE_POSITION, position);
      startActivity(intent);
  }*/
  
  public class ImageAdapter extends BaseAdapter {
  
      ArrayList&lt;String&gt; mList;
      LayoutInflater mInflater;
      Context mContext;
      SparseBooleanArray mSparseBooleanArray;
  
      public ImageAdapter(Context context, ArrayList&lt;String&gt; imageList) {
          // TODO Auto-generated constructor stub
          mContext = context;
          mInflater = LayoutInflater.from(mContext);
          mSparseBooleanArray = new SparseBooleanArray();
          mList = new ArrayList&lt;String&gt;();
          this.mList = imageList;
  
      }
  
      public ArrayList&lt;String&gt; getCheckedItems() {
          ArrayList&lt;String&gt; mTempArry = new ArrayList&lt;String&gt;();
  
          for(int i=0;i&lt;mList.size();i++) {
              if(mSparseBooleanArray.get(i)) {
                  mTempArry.add(mList.get(i));
              }
          }
  
          return mTempArry;
      }
  
      @Override
      public int getCount() {
          return imageUrls.size();
      }
  
      @Override
      public Object getItem(int position) {
          return null;
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
  
          if(convertView == null) {
              convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
          }
  
          CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
          final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
  
          imageLoader.displayImage("file://"+imageUrls.get(position), imageView, options, new SimpleImageLoadingListener() {
              @Override
              public void onLoadingComplete(Bitmap loadedImage) {
                  Animation anim = AnimationUtils.loadAnimation(MultiPhotoSelectActivity.this, R.anim.fade_in);
                  imageView.setAnimation(anim);
                  anim.start();
              }
          });
  
          mCheckBox.setTag(position);
          mCheckBox.setChecked(mSparseBooleanArray.get(position));
          mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
  
          return convertView;
      }
  
      OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
  
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              // TODO Auto-generated method stub
              mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
          }
      };
  }
  
  }
  </code></pre>