title: Android: ViewPager Gallery from sdcard
body: <p>I have some code for a ViewPager Gallery, however the images are taken from the drawable folder, whereas I need them from a folder on the sdcard.</p>
  
  <pre><code>protected Integer[] mImageIds = {
          R.drawable.sample_1,
          R.drawable.sample_2,
          R.drawable.sample_3,
          R.drawable.sample_4,
          R.drawable.sample_5,
          R.drawable.sample_6,
          R.drawable.sample_7
  };
  </code></pre>
  
  <p>This is the code that needs changing, how do I replace these from drawable folder with images from sdcard?</p>
  
  <p><strong>Full Code</strong></p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.os.Handler;
  import android.provider.MediaStore;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.*;
  
  public class Main extends FragmentActivity implements AdapterView.OnItemSelectedListener, ViewPager.OnPageChangeListener {
  
      private ViewPager viewPager;
      private Gallery gallery;
  
      protected Integer[] mImageIds = {
              R.drawable.sample_1,
              R.drawable.sample_2,
              R.drawable.sample_3,
              R.drawable.sample_4,
              R.drawable.sample_5,
              R.drawable.sample_6,
              R.drawable.sample_7
      };
      private TextView textView;
      private Handler handler;
  
      /**
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          viewPager = (ViewPager) findViewById(R.id.view_pager);
          viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
          viewPager.setOnPageChangeListener(this);
  
          gallery = (Gallery) findViewById(R.id.gallery);
          gallery.setAdapter(new ImageAdapter());
          gallery.setOnItemSelectedListener(this);
  
          textView = (TextView) findViewById(R.id.title);
  
          handler = new Handler();
  
      }
  
      // gallery item selected
      public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
          updateUI(i);
      }
  
      // view pager item selected
      public void onPageSelected(int i) {
          updateUI(i);
      }
  
      private void updateUI(final int i) {
          handler.post(new Runnable() {
              public void run() {
                  gallery.setSelection(i);
                  viewPager.setCurrentItem(i);
                  textView.setText("Photo #" + i);
              }
          });
      }
  
      public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {    }
  
      public void onPageScrolled(int i, float v, int i1) {    }
  
      public void onPageScrollStateChanged(int i) {    }
  
      public class ImageAdapter extends BaseAdapter {
  
          public int getCount() {
              return mImageIds.length;
          }
  
          public Object getItem(int position) {
              return position;
          }
  
          public long getItemId(int position) {
              return position;
          }
  
          public View getView(int position, View convertView, ViewGroup parent) {
  
              ImageView imageView = new ImageView(Main.this);
  
              imageView.setImageResource(mImageIds[position]);
              imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
              imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  
              return imageView;
          }
      }
  
      public class MyAdapter extends FragmentPagerAdapter {
          public MyAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public int getCount() {
              return mImageIds.length;
          }
  
          @Override
          public Fragment getItem(final int position) {
              return new Fragment() {
                  @Override
                  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                      ImageView imageView = new ImageView(Main.this);
                      imageView.setImageResource(mImageIds[position]);
                      return imageView;
                  }
              };
          }
      }
  
  
  }
  </code></pre>