0

基本上我有一个网格视图,它显示了一组来自可绘制资源的图像,但我希望它从我手机的 sd 卡中提取图像。

我得到一个空指针异常:return cursor.getCount(); 这也是:gridview.setAdapter(new ImageAdapter(this));

有人可以解释为什么它为空吗?我试图初始化光标,但我也遇到了麻烦。

public class MainActivity extends Activity {
ImageAdapter adapter;
private Cursor cursor;
private  int columnIndex;

@Override
public void onCreate(Bundle savedInstanceState) {
    View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] projection = {MediaStore.Images.Thumbnails._ID};
    cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID);
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView gridview = (GridView)view.findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    adapter.notifyDataSetChanged();

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            String[] projection = new String[]{MediaStore.Images.Media.DATA};
            cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Thumbnails._ID);
            columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
            cursor.moveToPosition(position);
            String imagePath = cursor.getString(columnIndex);
            //Toast below states position of image
            //Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            //Sending image id to fullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);

            //passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}

public void startCamera(View v){
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, 0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}




public class ImageAdapter extends BaseAdapter {
Context mContext;
ImageView imageview;
Cursor cursor;
int columnIndex;

public ImageAdapter(Context adapter) {
    mContext = adapter;
}

public int getCount() {
    return cursor.getCount();
    //return mThumbIds.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        cursor.moveToPosition(position);
        int imageID = cursor.getInt(columnIndex);
        imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));

        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    //imageView.setImageResource(mThumbIds[position]);
    //imageView.setImageDrawable(mySDCardImages.get(position).getDrawable());
    return imageView;
}

        // references to our images
        /* public Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, 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,
        R.drawable.sample_0, 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
};*/

}


      public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    //get intent data
    Intent i = getIntent();
    Cursor cursor = null;

    //selected image id
    int position = i.getExtras().getInt("id");
    MainActivity myActivity = new MainActivity();
    ImageAdapter imageAdapter = new ImageAdapter(this, cursor);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(position);
}

      }
4

1 回答 1

0

您的适配器类中有一个游标,该游标从未设置过其值,因此始终为空。删除声明并使用父级的光标。

于 2012-11-15T18:09:36.507 回答