0

我正在尝试将 sdcard 手机中的所有图像加载到 GridView 上。我实现了下面的代码,但它只加载了几张照片并重复了一百次。不知道我做错了什么!

这是 onCreate 方法:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid_images);

    //GridView
    imagesGridView = (GridView) findViewById(R.id.imagesGridView);

    //Do the query
    externalContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;        

    String[] projection = {MediaStore.Images.Media._ID}; 
    String selection = "";
    String [] selectionArgs = null;
    externalCursor = getContentResolver().query(externalContentUri,projection,selection,selectionArgs,null); 
    externalColumnIndex = externalCursor.getColumnIndex(MediaStore.Images.Media._ID);

    //Gets the adapter
    myAdapter = new ImageAdapter(this);
    imagesGridView.setAdapter(myAdapter);   
    imagesGridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(GridImages.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });

}

这是我的自定义适配器:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


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

    public int getCount() {
        return externalCursor.getCount();
    }

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

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

    // 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);
            externalCursor.moveToPosition(position);
            int imageID = externalCursor.getInt( externalColumnIndex );
            Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,Integer.toString(imageID));

            imageView.setImageBitmap(loadThumbnailImage(uri.toString()));   

        } else {
            imageView = (ImageView) convertView;
        }


        return imageView;
    }

    protected Bitmap loadThumbnailImage( String url ) {
        // Get original image ID
        int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));

        // Get (or create upon demand) the micro thumbnail for the original image.
        return MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
                            originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
    }

}
4

1 回答 1

1

你的getView方法应该是:

   if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
    } else {
         imageView = (ImageView) convertView;
    }
    externalCursor.moveToPosition(position);
    int imageID = externalCursor.getInt( externalColumnIndex );
    Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,Integer.toString(imageID))
    imageView.setImageBitmap(loadThumbnailImage(uri.toString()));   
    return imageView;
    }

使用您当前的代码,您仅在GridView需要图像时第一次设置图像(例如当它首次出现在屏幕上时),对于其他行,旧的会被回收。

于 2012-07-20T19:35:15.033 回答