在gridview中加载所有图像时出现Android错误

时间:2015-01-22 11:41:54

标签: android image gridview

我想制作一个图像库,其中包含网格视图中的所有图像。我已经成功地将sdcard中的图像加载到该图库中。但问题是相同的图像重复超过5次' 。这是我目前的代码,请帮我找到解决方案。

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

    GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
    sdcardImages.setAdapter(new ImageAdapter(this));
 }

这是我的ImageAdapter类

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_XY);
        picturesView.setPadding(10, 10, 10, 10);
        picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
    }
    else {
        picturesView = (ImageView)convertView;
    }
    return picturesView;
}

}

1 个答案:

答案 0 :(得分:0)

您可以使用仅{strong>外部存储的MediaStore.Images.Media.EXTERNAL_CONTENT_URI。对于内部,有MediaStore.Images.Media.INTERNAL_CONTENT_URI

我使用此代码从SD卡获取图像:

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

        GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
        sdcardImages.setAdapter(new ImageAdapter(this));

        // Set up a click listener
        sdcardImages.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                // Get the data location of the image
                String[] projection = {MediaStore.Images.Media.DATA};
                cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        projection, // Which columns to return
                        null,       // Return all rows
                        null,
                        null);
                columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToPosition(position);
                // Get image filename
                String imagePath = cursor.getString(columnIndex);
                // Use this path to do further processing, i.e. full screen display
            }
        });  

更新:

/**
     * Adapter for our image files.
     */
    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;
        }
    }

了解更多信息:
http://mobile.dzone.com/news/displaying-images-sd-card