title: Capture an image incombintion with GridView (ArrayAdapter)
body: <p>My code comprises of one grid view which will have several images in them as individual views. </p>
  
  <ol>
  <li><p>These images are to be either captured by a camera or selected from the gallery by the user</p></li>
  <li><p>Each view has an action allowing user to add an image to it or remove the view entirely from the adapter</p></li>
  <li>At any given time there may be 'X' amount of views with images loaded in them BUT only one view without an image (blank view to which user needs to load an image onto)</li>
  <li>I am achieving this by adding a new item to the adapter every time the user successfully adds an image to a view/row</li>
  </ol>
  
  <p>In Order to get <code>onActivityResult()</code> to work in combination with the <code>GridViewAdapter</code>, I have defined a private class of it within my activity itself allowing me to show the image picker dialog from within the adapter, and the <code>onActivityResult()</code> to be defined in the main activity.</p>
  
  <p>Following is my entire code</p>
  
  <pre><code>public class ImageGridView extends Activity {
  
      GridView gridView;
      GridViewAdapter gridViewAdapter;
      ArrayList&lt;GridItem&gt; list;
      Uri mUri;
      String imageLocation;
      ImageView imageView;
      public static final int IMAGE_CAPTURE =1337, IMAGE_GALLERY=12;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_image_grid_view);
          gridView = (GridView) findViewById(R.id.gridview);
          //This image view is used as a test to make sure a bitmap is being returned from onActivityResult
          imageView=(ImageView)findViewById(R.id.image_original);
  
          list = new ArrayList&lt;&gt;();
          GridItem gridItem = new GridItem();
          list.add(gridItem);
          gridViewAdapter = new GridViewAdapter(ImageGridView.this, R.layout.row_gridview, list);
          gridView.setAdapter(gridViewAdapter);
      }
  
      private class GridViewAdapter extends ArrayAdapter&lt;GridItem&gt; {
  
          private Context mContext;
          private int layoutResourceId;
          private ArrayList&lt;GridItem&gt; mGridData = new ArrayList&lt;GridItem&gt;();
  
          public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList&lt;GridItem&gt; mGridData) {
              super(mContext, layoutResourceId, mGridData);
              this.layoutResourceId = layoutResourceId;
              this.mContext = mContext;
              this.mGridData = mGridData;
          }
  
          public void setGridData(ArrayList&lt;GridItem&gt; mGridData) {
              this.mGridData = mGridData;
              notifyDataSetChanged();
          }
  
          @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
              View row = convertView;
              final ViewHolder holder;
  
              if (row == null) {
                  LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                  row = inflater.inflate(layoutResourceId, parent, false);
                  holder = new ViewHolder();
                  holder.btnAdd = (ImageView) row.findViewById(R.id.imgAdd);
                  holder.btnRemove = (ImageView) row.findViewById(R.id.imgRemove);
                  holder.imageView=(ImageView)row.findViewById(R.id.imgPrescriptionPicasso);
                  row.setTag(holder);
              } else {
                  holder = (ViewHolder) row.getTag();
              }
              // NOT sure if this is the correct way to go about it.
              // How do I add Uri of image from onActivityResult to this
              if(mGridData[position].getImageLocationAsString()!=null) {
                 String imageLocation = mGridData[position].getImageLocationAsString();
                 holder.imageView.setImageBitmap(BitmapFactory.decodeFile(imageLocation);
              holder.btnAdd.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      selectImage();
  
                  }
              });
  
              holder.btnRemove.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      mGridData.remove(position);
                      notifyDataSetChanged();
                      //Remove this view entirely and notify adapter of the change
                  }
              });
              return row;
          }
           class ViewHolder {
              ImageView imageView;
              ImageView btnAdd, btnRemove;
               //The buttons have been defined as image Views for now.
          }
      }
  
      private void selectImage() {
          final CharSequence[] items = {"Take Photo", "Choose from Gallery",
                  "Cancel"};
          AlertDialog.Builder builder = new AlertDialog.Builder(ImageGridView.this);
          builder.setTitle("Add picture of Prescription");
          builder.setItems(items, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int item) {
                  if (items[item].equals("Take Photo")) {
  
                      File f = getOutputMediaFile();
                      mUri = Uri.fromFile(f);
                      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                      intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
                      startActivityForResult(intent, IMAGE_CAPTURE);
  
                      Log.d("Clicked", "Take photo");
                  } else if (items[item].equals("Choose from Gallery")) {
  
                      Intent intent = new Intent(
                              Intent.ACTION_PICK,
                              android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                      intent.setType("image/*");
                      startActivityForResult(
                              Intent.createChooser(intent, "Select File"),
                              IMAGE_GALLERY);
  
                  }
              }
          });
          builder.show();
      }
  
      private File getOutputMediaFile() {
          File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_DCIM), "Medit");
          if (!mediaStorageDir.exists()) {
              mediaStorageDir.mkdirs();
          }
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
          File mediaFile;
          mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                  "IMG" + timeStamp + ".jpg");
          Log.d("reached a", mediaFile.toString());
          return mediaFile;
      }
  
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data){
          super.onActivityResult(requestCode, resultCode, data);
          if (resultCode== Activity.RESULT_OK){
              switch(requestCode) {
                  case IMAGE_CAPTURE:
                      Bitmap bitmap= BitmapFactory.decodeFile(mUri.getPath());
                      imageView.setImageBitmap(bitmap);
                      //If user loads an image, a new view is created with a blank image
                      GridItem gridItem = new GridItem();
                      list.add(gridItem);
                      gridViewAdapter.notifyDataSetChanged();
                      break;
  
                  case IMAGE_GALLERY:
                      Uri selectedImageUri = data.getData();
                      String[] filePathColumn = { MediaStore.Images.Media.DATA };
                      Cursor cursor = getContentResolver().query(selectedImageUri,
                              filePathColumn, null, null, null);
                      cursor.moveToFirst();
                      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                      imageLocation = cursor.getString(columnIndex);
                      cursor.close();
                      Bitmap bitmap1= BitmapFactory.decodeFile(imageLocation);
                      imageView.setImageBitmap(bitmap1);
                      //If user loads an image, a new view is created with a blank image
                      GridItem gridItem1 = new GridItem();
                      list.add(gridItem1);
                      gridViewAdapter.notifyDataSetChanged();
                      break;
              }//Close switch statement
  
          }else {
              Log.d("Error ", "user canceled !");
          }
      }
  
  }
  </code></pre>
  
  <p>The <code>imageView</code> defined in my <code>Activity</code> is being set in <code>onActivityResult()</code> just to test whether that a bitmap is actually being returned or not.</p>
  
  <p>I need help with</p>
  
  <ol>
  <li>In the create <code>view holder</code>, set a bitmap to the image view within, if the user has set one</li>
  <li>I thought of either somehow setting the image of a specific grid item in the <code>onActivityResult()</code> code itself.</li>
  </ol>
  
  <p>GridItem currently has a String in it which I am trying to use, to store the image location of the image, if it exists and load the bitmap in the <code>getView()</code> holder function of the <code>GridViewAdapter</code> but am stuck over here.</p>
  
  <p>The final aim of this is to let users add as many images to the <code>GridViewAdapter</code> and finally send the <code>Uri.getPath()</code> (imageLocation as String) via an intent to the next activity for which I will be using an array.</p>
  
  <p>Will really appreciate any help/ guidance I can get for the same. Thanks</p>