title: Display Images in grid view after selecting from gallery
body: <p>I am writing code to select images from the gallery and later to display them on a grid view . I had done the code till allowing the user to select images from the gallery. Now I need help to further move this and display the selected images in a separate gird view . Here I am attaching my code till what I have done.Thanks in advance.</p>
  
  <p>MultiPhotoSelectActivity.java</p>
  
  <pre><code>package com.mamakar.gridviewimplementation;
  
  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.CompoundButton.OnCheckedChangeListener;
  import android.widget.GridView;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  import com.nostra13.universalimageloader.core.DisplayImageOptions;
  import com.nostra13.universalimageloader.core.ImageLoader;
  import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
  
  import java.util.ArrayList;
  
  /**
   * @author Paresh Mayani (@pareshmayani)
   */
  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);
  
          //Bundle bundle = getIntent().getExtras();
          //imageUrls = bundle.getStringArray(Extra.IMAGES);
  
          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");
      //MultiPhotoSelectActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseCont‌​ext()));
  
      ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(MultiPhotoSelectActivity.this));
      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>
  
  <p>BaseActivity.java</p>
  
  <pre><code>package com.mamakar.gridviewimplementation;
  
  import android.app.Activity;
  
  import com.nostra13.universalimageloader.core.ImageLoader;
  
  /**
   * @author Paresh Mayani (@pareshmayani)
   */
  public abstract class BaseActivity extends Activity {
  
      protected ImageLoader imageLoader = ImageLoader.getInstance();
  
  }
  </code></pre>
  
  <p>UILApplication.java</p>
  
  <pre><code>package com.mamakar.gridviewimplementation;
  
  import android.app.Application;
  
  import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
  import com.nostra13.universalimageloader.core.ImageLoader;
  import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  
  /**
   * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
   */
  public class UILApplication extends Application {
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      // This configuration tuning is custom. You can tune every option, you may tune some of them, 
      // or you can create default configuration by
      //  ImageLoaderConfiguration.createDefault(this);
      // method.
      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
          .threadPoolSize(3)
          .threadPriority(Thread.NORM_PRIORITY - 2)
          .memoryCacheSize(1500000) // 1.5 Mb
          .denyCacheImageMultipleSizesInMemory()
          .discCacheFileNameGenerator(new Md5FileNameGenerator())
          .enableLogging() // Not necessary in common
          .build();
      // Initialize ImageLoader with configuration.
      ImageLoader.getInstance().init(config);
  //  BaseActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseCont‌​ext()));
  }
  }
  </code></pre>
  
  <p>ac_image_grid.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" &gt;
  
      &lt;GridView
          android:id="@+id/gridview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:columnWidth="100dip"
          android:gravity="center"
          android:horizontalSpacing="4dip"
          android:numColumns="auto_fit"
          android:stretchMode="columnWidth"
          android:layout_above="@+id/button1"
          android:verticalSpacing="2dip" /&gt;
  
      &lt;Button
          android:id="@+id/button1"
          android:layout_alignParentBottom="true"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:onClick="btnChoosePhotosClick"
          android:text="Select Photos" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>row_multiphoto_item.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" &gt;
  
  &lt;ImageView
      android:id="@+id/imageView1"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:src="@mipmap/ic_launcher" /&gt;
  
  &lt;CheckBox
      android:id="@+id/checkBox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/imageView1"
      android:layout_centerVertical="true"/&gt;
  </code></pre>
  
  <p></p>