Displaying pictures from the SD card in a gridview
<p>I am having a lot of problems displaying pictures from the SD card in a GridView. I am writing the code for an application that is supposed to allow the user to display images from the SD card in a gridview. The code I have so far crashes every time although there are no errors being picked by the compiler. Here it is:</p>

<pre><code>package com.newtest1;

import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.view.View;
import android.widget.GridView;
import android.widget.Adapter;
import android.widget.AdapterView;

import android.widget.Toast;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;

import android.net.Uri;

import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {

    public Cursor myImageCursor;
    public int columnNumber;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



String[] imageIDs = new String[]{Thumbnails._ID};

    Uri myImagesSource = Thumbnails.EXTERNAL_CONTENT_URI;

   myImageCursor = managedQuery(myImagesSource, 
                imageIDs, null, null, MediaStore.Images.Thumbnails._ID);

     columnNumber = myImageCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView PhoneImageView = (GridView)findViewById(R.id.photogrid);
    PhoneImageView.setAdapter(new ImageAdapter(this));

    PhoneImageView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView&lt;?&gt; parent, View v,
int position, long id) {



              String[] data = { MediaStore.Images.Media.DATA };

             Cursor  viewImageCursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, data,
null, null, MediaStore.Images.Thumbnails._ID );
           int imageColumnIndex = viewImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       viewImageCursor.moveToPosition(position);
       String filepath = viewImageCursor.getString(imageColumnIndex);
       Toast.makeText(MainActivity.this, filepath, Toast.LENGTH_LONG).show();



         }
   });
    }
}


package com.newtest1;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Thumbnails;

public class ImageAdapter extends BaseAdapter{

final   MainActivity pca = new MainActivity();



    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }

    public int getCount() {
        return pca.myImageCursor.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
            pca.myImageCursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = pca.myImageCursor.getInt(pca.columnNumber);
            // 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;
    }
}

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            &gt;





    &lt;GridView 
    android:id="@+id/photogrid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/&gt;
&lt;/LinearLayout&gt;
</code></pre>

<p>Can somebody please help me as soon as possible? Any help would be much appreciated. </p>
