title: Same images are appearing multiple times in grid view
body: <p>I had developed an album app in which I am using grid View. The app is working but every time when I press back button and return to my app, one more copy of image is appearing in  the grid view. For instance, I have 7 images in the grid view, when I press back button and then return to app, it becomes 14. Next time total images will be 21 and so on. I am reading images from SD card. My app is having full screen functionality, so same thing occurs that case as well. I am not getting why it happened. Below, I am posting my code:</p>
  
  <p><b>Album Activity</b> </p>
  
  <pre><code>public class Album3Activity extends Activity {
  static File [] mediaFiles;
  static File imageDir;
  static GridView gridView;
  ImageAdapter adapter;
  Intent in;
  static String name = null;
  static ArrayList&lt;Bitmap&gt; bmpArray = new ArrayList&lt;Bitmap&gt;();
  static ArrayList&lt;String&gt; fileName = new ArrayList&lt;String&gt;();
  public static final String TAG = "Album3Activity";
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.grid);
      imageDir = new File(Environment.getExternalStorageDirectory().toString()+
               "/diplomat");
      mediaFiles = imageDir.listFiles();
      //Log.d("Lenght of images",""+mediaFiles.length);
      for(File file : mediaFiles)
      {
          bmpArray.add(convertToBitmap(file));
          fileName.add(readFileName(file));
          Log.d(TAG + "bmpArray Size", ""+bmpArray.size());
          Log.d(TAG, "call to convertToBitmap");
      }//for
  
      adapter = new ImageAdapter(this, bmpArray, fileName);
      gridView = (GridView)findViewById(R.id.gridview);
      gridView.setAdapter(adapter);
      gridView.setOnItemClickListener(new OnItemClickListener() {
  
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int      position,
                  long arg3) {
              in = new Intent(getApplicationContext(), FullScreen.class);
              in.putExtra("id", position);
              startActivity(in);
          }
      });
  }//onCreate
  
  public static Bitmap convertToBitmap(File file) 
  {
      URL url = null;
      try 
      {
          url = file.toURL();
      } catch (MalformedURLException e1) 
      {
          Log.d(TAG, e1.toString());
      }//catch
  
      Bitmap bmp = null;
      try
      {
         bmp = BitmapFactory.decodeStream(url.openStream());
         //bmp.recycle();
      }catch(Exception e)
      {
          Log.d(TAG, "Exception: "+e.toString());
      }//catch
      return bmp;
  }//convertToBitmap
  
  public String readFileName(File file){
      String name = file.getName();
      return name;
  }//readFileName
  }//class
  </code></pre>
  
  <p><b>Image Adapter</b></p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
  Activity act;
  ArrayList&lt;Bitmap&gt; mFiles;
  static ArrayList&lt;String&gt; mName;
  public static final String TAG = "ImageAdapter";
  static String name = null;
  public ImageAdapter(Activity act, ArrayList&lt;Bitmap&gt; mFiles, ArrayList&lt;String&gt;     mName) {
      super();
      this.act = act;
      this.mFiles = mFiles;
      ImageAdapter.mName = mName;
      Log.d("mfiles", "" + this.mFiles.size());
  }// ImageAdapter
  
  public int getCount() {
      return mFiles.size();
  }// getCount
  
  public Object getItem(int postion) {
      return mFiles.get(postion);
  }// getItem
  
  public long getItemId(int position) {
      return 0;
  }// getItemId
  
  public static class ViewHolder {
      ImageView iv;
      TextView tv;
  }
  
  public View getView(int position, View convertView, ViewGroup parent) {
  
      ViewHolder view;
      LayoutInflater li = act.getLayoutInflater();
      if (convertView == null) {
          Log.d("tag", "null");
          view = new ViewHolder();
          convertView = li.inflate(R.layout.gridview_row, null);
          view.iv = (ImageView) convertView.findViewById(R.id.imageView1);
          view.tv = (TextView) convertView.findViewById(R.id.textView1);
          convertView.setTag(view);
      }// if
      else {
          view = (ViewHolder) convertView.getTag();
      }// else
      view.iv.setImageBitmap(mFiles.get(position));
      view.tv.setText(readName(position));
      Log.d(TAG, "getView method called");
      return convertView;
  }// getView
  
  public static String  readName(int position) {
      String name = mName.get(position);
      return name;
  }//readName
  
      }// ImageAdapter
  </code></pre>
  
  <p><b>FullScreen.java</b></p>
  
  <pre><code>public class FullScreen extends Activity implements OnClickListener, OnGestureListener{
  ImageView imageView1;
  ViewSwitcher viewSwitcher;
  ImageAdapter adapter = new ImageAdapter(getParent(), Album3Activity.bmpArray, Album3Activity.fileName);
  Button avbryt, camera, trash, opendialog, slett, 
      avbryt1, epost, mms, avbryt2, facebook;
  TextView textView;
  ImageButton btnNext, btnPrevious;
  GestureDetector gesturedetector = null;
  Intent in; 
  int touches; 
      protected void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fullscreen);
      viewSwitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher);
      imageView1 = (ImageView)findViewById(R.id.imageView1);
      textView = (TextView)findViewById(R.id.textView1);
  
      in = getIntent();
      touches = in.getExtras().getInt("id");
      imageView1.setImageBitmap((Bitmap)adapter.getItem(touches)); 
      textView.setText(ImageAdapter.readName(touches));
  
      btnNext = (ImageButton)findViewById(R.id.btnNext);
      btnNext.setOnClickListener(this);
  
      btnPrevious = (ImageButton)findViewById(R.id.btnPrevious);
      btnPrevious.setOnClickListener(this);
  
      trash = (Button)findViewById(R.id.trash);
      trash.setOnClickListener(this);
  
      opendialog = (Button)findViewById(R.id.opendialog);
      opendialog.setOnClickListener(this);
  
      avbryt = (Button)findViewById(R.id.avbryt);
      avbryt.setOnClickListener(this);
  
      camera = (Button)findViewById(R.id.camera_albumbutton);
      camera.setOnClickListener(this);
  
      gesturedetector=new GestureDetector(this, this);
  }//onCreate
  
      public void onClick(View v) {
  
      switch (v.getId()) {
      case R.id.btnNext:
          Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next);
          btnNext.startAnimation(animationNext);
          nextView();
          break;
  
      case R.id.btnPrevious:
          Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
          btnPrevious.startAnimation(animationPrevious);
          previousView();
          break;
  
      case R.id.trash:
          final Dialog dialog  = new Dialog(FullScreen.this);
          dialog.setContentView(R.layout.trashdialog);
          slett = (Button)dialog.findViewById(R.id.slett);
          avbryt1 = (Button)dialog.findViewById(R.id.avbryt);
  
          slett.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  File file = new File(Environment.getExternalStorageDirectory()+
                              "/diplomat/"+ ImageAdapter.readName(touches));
                  Log.d("FullScreen Path", file.getAbsolutePath());
                  Log.d("Hi1 touches value", ""+touches);
                  try
                  {
  
                      if(touches == (Album3Activity.bmpArray.size() - 1))
                      {
                          file.delete();
                          sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                  , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
                          Album3Activity.bmpArray.remove(touches);
                          Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());
  
                          Album3Activity.fileName.remove(touches);
                          Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());
  
                          Album3Activity.gridView.invalidateViews();
                          adapter.notifyDataSetChanged();
  
                          touches = Album3Activity.bmpArray.size() - touches;
                          Log.d("FullScreen delete:if block", "Updated touches value:"+touches);
  
                          do
                          {
                              Log.d("FullScreen:delete if", "in while loop");
                              imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                              textView.setText(ImageAdapter.readName(touches));
                              break;
                          }//do
                          while(Album3Activity.bmpArray.isEmpty() == false);
                      }//if
                      else
                      {
                          Log.d("fullscreen:delete else", "else block executed");
                          file.delete();
                          sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED
                                  , Uri.parse(Environment.getExternalStorageDirectory().toString()+"/diplomat")));
  
                          Album3Activity.bmpArray.remove(touches);
                          Log.d("Hi2 new bmpArray size", ""+Album3Activity.bmpArray.size());
  
                          Album3Activity.fileName.remove(touches);
                          Log.d("Hi2 new fileName size", ""+Album3Activity.fileName.size());
  
                          Album3Activity.gridView.invalidateViews();
                          adapter.notifyDataSetChanged();
  
                          imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
                          textView.setText(ImageAdapter.readName(touches));
  
                      }//else
                  }//try
                  catch(IndexOutOfBoundsException e)
                  {
                      Log.d("Fullscreen delete method", e.toString());
                      Toast.makeText(FullScreen.this, "No more image left to diaplay"
                              , Toast.LENGTH_SHORT).show();
                  }//catch
                  finally
                  {
                      dialog.dismiss();
                  }//finally
              }//onClick
          });
  
          avbryt1.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  dialog.cancel();
              }//onClick
          });
          dialog.show();
          break;
  
      case R.id.opendialog:
          final Dialog dialog1 = new Dialog(FullScreen.this);
          dialog1.setContentView(R.layout.openbuttondialog);
          epost = (Button)dialog1.findViewById(R.id.epost);
          mms = (Button)dialog1.findViewById(R.id.mms);
          facebook = (Button)dialog1.findViewById(R.id.facebook);
          avbryt2 =(Button)dialog1.findViewById(R.id.openavbryt);
  
          epost.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
              }//onClick
          });
  
          mms.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
              }//onClick
          });
  
          facebook.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
              }//onClick
          });
  
          avbryt2.setOnClickListener(new View.OnClickListener() {
  
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  dialog1.cancel();
              }//onClick
          });
          dialog1.show();
          break;
  
      case R.id.avbryt:
          break;
  
      case R.id.camera_albumbutton:
          break;
  
      default:
          break;
      }//switch
  
  }//onClick
  
  private void previousView() {
      if(touches &gt; 0){
          touches = touches - 1;
          imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
          textView.setText(ImageAdapter.readName(touches));
          viewSwitcher.setInAnimation(this, R.anim.in_animation1);
          viewSwitcher.setOutAnimation(this, R.anim.out_animation1);
          viewSwitcher.showPrevious();
      }//if
      else{
          Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
      }//else
      Log.d("Switcher", "previousView");
  }//previousView
  
  private void nextView() {
      if(touches &lt; Album3Activity.bmpArray.size() - 1){
          touches = touches + 1;
          imageView1.setImageBitmap((Bitmap)adapter.getItem(touches));
          textView.setText(ImageAdapter.readName(touches));
          viewSwitcher.setInAnimation(this, R.anim.in_animation);
          viewSwitcher.setOutAnimation(this, R.anim.out_animation);
          viewSwitcher.showNext();
      }//if
      else{
          Toast.makeText(this, "No image left", Toast.LENGTH_SHORT).show();
      }//else
      Log.d("Switcher", "nextView");
  }//nextView
  
  public boolean onDown(MotionEvent e) {
      // TODO Auto-generated method stub
      return true;
  }
  
  
  int SWIPE_MIN_VELOCITY = 50;
  int SWIPE_MIN_DISTANCE = 50;
  
  
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
          float velocityY) {
      float ev1X = e1.getX();
      float ev2X = e2.getX();
      Log.d("On", "Onfling");
      //Get distance of X (e1) to X (e2)
      final float xdistance = Math.abs(ev1X - ev2X);
      //Get veclocity of cusor
      //Váº­n tá»‘c = sá»‘ Ä‘iá»ƒm áº£nh (px) / giÃ¢y
      final float xvelocity = Math.abs(velocityX);
  
      //Váº­n tá»‘c chuyá»ƒn Ä‘á»•i X &gt; 100 vÃ  khoáº£ng cÃ¡ch tá»« Ä‘iá»ƒm kÃ©o Ä‘áº§u Ä‘áº¿n Ä‘iá»ƒm kÃ©o cuá»‘i &gt; 100
      if( (xvelocity &gt; SWIPE_MIN_VELOCITY) &amp;&amp; (xdistance &gt; SWIPE_MIN_DISTANCE) )
      {
          if(ev1X &gt; ev2X)//Switch Left
          {
              previousView();
          }
          else//Switch Right
          {
              nextView();
          }
      }
  
      return false;
  }
  
  public void onLongPress(MotionEvent e) {
      // TODO Auto-generated method stub
  
  }
  
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
          float distanceY) {
      // TODO Auto-generated method stub
      return false;
  }
  
  public void onShowPress(MotionEvent e) {
      // TODO Auto-generated method stub
  
  }
  
  public boolean onSingleTapUp(MotionEvent e) {
      // TODO Auto-generated method stub
      return false;
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      Log.d("on touch","touch");
      // TODO Auto-generated method stub
      //return gesturedetector.onTouchEvent(event);
  
      this.gesturedetector.onTouchEvent(event);
        return super.onTouchEvent(event);
  }
  
      }//FullScreen
  </code></pre>