title: Unable to display the image selected in gallery view
body: <p>I have an issue with showing the preview of image selected in the gallery view. I have a gallery of images from sd card, on clicking an image its preview should be shown below the gallery view(Not in seperate activity via intent). I am able to show the gallery with images but nothing is happening on clicking image. </p>
  
  <p>public class NewActivity extends Activity {</p>
  
  <pre><code>GalleryBaseAdapter myGalleryBaseAdapter;
  Gallery myPhotoGallery;
  int[] mFiles = null;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);      
  
      myPhotoGallery = (Gallery)findViewById(R.id.photogallery);
  
      myGalleryBaseAdapter = new GalleryBaseAdapter(this);
  
      String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();
  
      String path = ExternalStorageDirectoryPath;
      String  files;
      File  folder = new File (path);
      final File[] listOfFiles = folder.listFiles(); 
  
      for (int i = 0; i &lt; listOfFiles.length; i++) 
      {       
           if (listOfFiles[i].isFile()) 
           {
               files = listOfFiles[i].getName();
               System .out.println(files);
           }
      }
  
      for (File file : listOfFiles) {
          myGalleryBaseAdapter.add(file.getPath());
      }        
  
      myPhotoGallery.setAdapter(myGalleryBaseAdapter);       
  
  
      myPhotoGallery.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView parent, View v, int position, long id) {
  
              mFiles = new int[listOfFiles.length];       
  
              ImageView imageview = (ImageView)findViewById(R.id.imageView1);     
              Bitmap bitmapImage = BitmapFactory.decodeFile("/sdcard/" + mFiles[position]);
              imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
              imageview.setImageBitmap(bitmapImage); 
  
          }                   
      });
  }
  
  public class GalleryBaseAdapter extends BaseAdapter {
  
      ArrayList&lt;String&gt; GalleryFileList;
      Context context;
  
      GalleryBaseAdapter(Context cont){
          context = cont;
          GalleryFileList = new ArrayList&lt;String&gt;();    
      }
  
      @Override
      public int getCount() {
          return GalleryFileList.size();
      }
  
      @Override
      public Object getItem(int position) {
          return GalleryFileList.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          Bitmap bm = BitmapFactory.decodeFile(GalleryFileList.get(position));
  
          LinearLayout layout = new LinearLayout(context);
          layout.setLayoutParams(new Gallery.LayoutParams(150, 150));
          layout.setGravity(Gravity.CENTER);
  
          ImageView imageView = new ImageView(context);
          imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          imageView.setImageBitmap(bm);
  
          layout.addView(imageView);
          return layout;
      }
  
      public void add(String newitem){
          GalleryFileList.add(newitem);
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Can anybody please tell me what is the mistake in my code. Thanks in advance.</p>