图库不滚动

时间:2012-03-29 13:18:47

标签: android android-gallery android-cursor

这是两部分的求助。我正在创建一个图像库,我将从SD卡上的特定文件夹中删除。我创建了一个GalleryView类:

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);


    }



}

和一个名为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;
    }

}

第一个问题是getCount方法返回一个空指针异常: 它不允许我滚动图像..

我的问题的第二部分是如何指向特定文件夹..“/ LC / images /”我假设它会发生在我的图像适配器中:

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

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

在使用之前不要初始化游标,可以使用CursorAdapter或在构造函数中传递它