I have created a gallery. I am populating a gridview from a simple cursor adapter (custom implementation).
First issue is that gridview stucks in the middle while scrolling. I think its because images are binded to a view continuously on user interaction. How can I achieve a smooth scroll?
Secondly I do not get all the images from the gallery through the Cursor. What do I need to do in order to get all the images. I also observed that this code does not return images on different devices, such as Samsung Galaxy s2. Works fine on Samsung Note 2 and Nexus 4 though. URI is same though: content://media/external/images/thumbnails
Code for an adapter:
/**
* @author Syed Ahmed Hussain
*/
public class CustomGalleryAdapter extends SimpleCursorAdapter {
private ContentResolver mContentResolver;
private LayoutInflater mLayoutInflater;
private int mImageIdColumnIndex;
private ImageView mImageView;
private Context mContext;
private int mLayout;
public CustomGalleryAdapter(Context pContext, int pLayout, Cursor pCursor, String[] pFrom, int[] pTo, int pFlags) {
super(pContext, pLayout, pCursor, pFrom, pTo, pFlags);
mLayout = pLayout;
mContext = pContext;
mLayoutInflater = LayoutInflater.from(mContext);
mContentResolver = mContext.getContentResolver();
mImageIdColumnIndex = pCursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
}
@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) {
long id = pCursor.getLong(mImageIdColumnIndex);
mImageView = (ImageView) pView;
mImageView.setScaleType(ScaleType.CENTER_CROP);
mImageView.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, new Options()));
}
}
Code for an activity:
public class GalleryActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private String[] projection = { MediaStore.Images.Thumbnails.IMAGE_ID };
private CustomGalleryAdapter mCustomGalleryAdapter;
private String[] selectionArgs = null;
private String selection = "";
private GridView mGridView;
@Override
protected void onCreate(Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
setContentView(R.layout.view_gallery);
mGridView = (GridView) findViewById(R.id.gridView);
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int pId, Bundle pArgs) {
return new android.support.v4.content.CursorLoader(getApplicationContext(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> pLoader, Cursor pData) {
mCustomGalleryAdapter = new CustomGalleryAdapter(getApplicationContext(), R.layout.gallery_item, pData, projection, new int[] { R.id.galleryImageView }, 0);
mGridView.setAdapter(mCustomGalleryAdapter);
}
@Override
public void onLoaderReset(Loader<Cursor> pLoader) {
}
}
View_Galler.xml:
<?xml version="1.0" encoding="utf-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:numColumns="auto_fit"
android:columnWidth="100dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:layoutMode="opticalBounds"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:listSelector="#00000000"
android:id="@+id/gridView" />
gallery_item.xml:
<?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" />
Can anyone please guide me on what am I doing wrong? and how can I solve these problems?