title: Load Image from Specific SDCard folder in GridView
body: <p>I am trying to develop an app that creates images within a folder in the SDCard and displays them ("MyCreatedImages" folder which now shows up in gallery) and now I am trying to load all the images from that given folder within a gridview layout. I am using the following code however I keep getting a NPE for reasons I cannot understand. </p>
  
  <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View root = inflater.inflate(R.layout.activity_my_image_fragment,
              container, false);
  
      // Set up an array of the Thumbnail Image ID column we want
      String[] projection = { MediaStore.Images.Thumbnails._ID };
  
      // Create the cursor pointing to the SDCard
      cursor = getActivity().getContentResolver().query(
              Uri.parse("file://" + Environment.getExternalStorageDirectory()+ File.separator + "MyCreatedImages"), projection,
              null, null, MediaStore.Images.Thumbnails._ID);
  
      // Get the column index of the Thumbnails Image ID
      columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
  
      GridView gridview = (GridView) root.findViewById(R.id.gridview);
      gridview.setAdapter(new ImageAdapter(this.getActivity()));
      //this.registerForContextMenu(gridview);
      return root;
  }
  </code></pre>
  
  <p>And my Gridview adapter:</p>
  
  <pre><code>private class ImageAdapter extends BaseAdapter {
      private Context context;
  
      public ImageAdapter(Context localContext) {
          context = localContext;
      }
  
      public int getCount() {
          return cursor.getCount();
      }
  
      public Object getItem(int position) {
          return position;
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView picturesView;
          if (convertView == null) {
              picturesView = new ImageView(context);
              // Move cursor to current position
              cursor.moveToPosition(position);
              // Get the current value for the requested column
              int imageID = cursor.getInt(columnIndex);
              // Set the content of the image based on the provided URI
              picturesView.setImageURI(Uri.withAppendedPath(
                      MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                      "" + imageID));
              picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
              picturesView.setPadding(8, 8, 8, 8);
              picturesView
                      .setLayoutParams(new GridView.LayoutParams(100, 100));
          } else {
              picturesView = (ImageView) convertView;
          }
          return picturesView;
      }
  }
  </code></pre>
  
  <p>Upon opening this fragment I get the following LogCat errors:</p>
  
  <pre><code>09-23 16:25:06.286: E/AndroidRuntime(28497): FATAL EXCEPTION: main
  09-23 16:25:06.286: E/AndroidRuntime(28497): java.lang.NullPointerException
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at com.meme.hdmeme.MyMemeFragment.onCreateView(MyMemeFragment.java:51)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.os.Handler.handleCallback(Handler.java:615)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.os.Handler.dispatchMessage(Handler.java:92)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.os.Looper.loop(Looper.java:137)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at android.app.ActivityThread.main(ActivityThread.java:4921)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at java.lang.reflect.Method.invokeNative(Native Method)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at java.lang.reflect.Method.invoke(Method.java:511)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  09-23 16:25:06.286: E/AndroidRuntime(28497):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Line 51 is :</p>
  
  <pre><code>    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
  </code></pre>