Search code examples
androidandroid-galleryandroid-cursor

Gallery not scrolling


This is a two part plead for help. I am creating a Gallery of images that i will be pulling off a specific folder on the sdcard. I have created a GalleryView class:

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @author elidd1
 *
 */
public class GalleryView extends Activity{

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

        // 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 = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
       // Get the column index of the Thumbnails Image ID
       columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);



    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new GallImageAdapter(this,cursor,columnIndex));

        imageView = (ImageView)findViewById(R.id.ImageView01);


    }



}

and a custom Image adapter called GallImageAdapter:

import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GallImageAdapter extends BaseAdapter {
    public Cursor cursor;
    private int columnIndex;
    private Context context;
    int imageBackground;

    public GallImageAdapter(Context ctx, Cursor cur, int cIn) {
    context = ctx;
    columnIndex = cIn;
    cursor = cur;
}

    @Override
    public int getCount() {

        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    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);
            int imageID = cursor.getInt(columnIndex);

            picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,""+imageID));

            picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            picturesView.setPadding(10,10,10,10);

        }else{
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }

}

first problem is that the getCount method is returning a null pointer exception: it is not allowing me to scroll through the images..

The second part of my question is how do i point to a specific folder.. "/LC/images/" i am assuming it would happen in my image adapter in this line:

picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,""+imageID)); 

thanks for any help


Solution

  • You don't initialize a cursor before using it, You can use CursorAdapter or pass it in constructor