0

I am trying to create a GalleryView. Im populating a GridView from a Custom implementation of SimpleCursor Adapter. I dont see anything after setting an adapter to a gridview. I can only see a white screen.

I think I am not binding the view properly. GalleryView is a fragment which uses the CustomGalleryAdapter.

public class GalleryView extends BaseFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private String[] projection = { MediaStore.Images.Thumbnails._ID };
    private CustomGalleryAdapter mCustomGalleryAdapter;
    private String[] selectionArgs = null;
    private String selection = "";
    private GridView mGridView;

    @Override
    public View onCreateView(LayoutInflater pInflater, ViewGroup pContainer, Bundle pSavedInstanceState) {
        mGridView = new GridView(getActivity());
        getLoaderManager().initLoader(0, null, this);

        return mGridView;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int pId, Bundle pArgs) {

        return new android.support.v4.content.CursorLoader(getActivity(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                                 null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> pLoader, Cursor pData) {
        mCustomGalleryAdapter = new CustomGalleryAdapter(getActivity(), R.layout.gallery_item, pData, projection, new int[] { R.id.galleryImageView }, 0);
        mGridView.setAdapter(mCustomGalleryAdapter);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> pLoader) {

    }

}

Following is an adapter.

/**
 * @author Syed Ahmed Hussain
 */
public class CustomGalleryAdapter extends SimpleCursorAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int mLayout;
    private Cursor mCursor;
    ImageView mImageView;
    ContentResolver mContentResolver;

    public CustomGalleryAdapter(Context pContext, int pLayout, Cursor pCursor, String[] pFrom, int[] pTo, int pFlags) {
        super(pContext, pLayout, pCursor, pFrom, pTo, pFlags);
        mContext = pContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        mLayout = pLayout;
        mCursor = pCursor;
        mContentResolver = mContext.getContentResolver();
    }

    @Override
    public View newView(Context pContext, Cursor pCursor, ViewGroup pParent) {
        return mLayoutInflater.inflate(mLayout, null);
    }

    @Override
    public void bindView(View pView, Context pContext, Cursor pCursor) {
        super.bindView(pView, pContext, pCursor);
        if (pView instanceof ImageView) {
            mImageView = (ImageView) pView;
        }

        int id = pCursor.getInt(0);

        mImageView.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, new Options()));

    }
}

GalleryItem Layout -

<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/galleryImageView"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
4

2 回答 2

0

Your not getting the image id correctly try:

int index = pCursor.getColumnIndex(MediaStore.Images.Media._ID);
long id = pCursor.getLong(index);

You should also get the thumbnail on a different thread since it's blocking

于 2013-10-18T17:58:06.403 回答
0

So the right answer is,

pCursor.getColumnIndex(MediaStore.Images.Thumbnail.IMAGE_ID);

YAY!!!! 10 points for me :D

于 2013-10-18T18:03:59.837 回答