I am trying to solve a somewhat tricky issue. I have a gridview that gets images from a remote server via a custom BaseAdapter. Relevant code below.
//The gridview
pictureAdapter = new PictureAdapter(cont, document, thumbWidth);
GridView gridview = (GridView) findViewById(R.id.pictures_gridview);
gridview.setColumnWidth(thumbWidth);
gridview.setAdapter(pictureAdapter);
The definition of the gridview is pretty straightforward for this...
//The adapter
public class PictureAdapter extends BaseAdapter {
private Context mContext;
private JSONArray mPics;
private List<String> mThumbs;
private List<String> mViews;
private List<Integer> mIds;
private int thumbWidth;
private SparseArray<Bitmap> imageData;
private boolean isFlinging;
public PictureAdapter(Context c, JSONArray pics, int thumbWidth) {
mContext = c;
mPics = pics;
this.thumbWidth = thumbWidth;
setPicThumbs();
imageData = new SparseArray<Bitmap>();
}
public void setFlinging(boolean isFlinging) {
this.isFlinging=isFlinging;
}
public boolean getFlinging() {
return this.isFlinging;
}
private void setPicThumbs() {
mThumbs = new Vector<String>();
mViews = new Vector<String>();
mIds = new Vector<Integer>();
for(int i=0; i<mPics.length(); i++) {
JSONObject row;
try {
row = mPics.getJSONObject(i);
mThumbs.add(row.getString("thumb"));
mViews.add(row.getString("view"));
mIds.add(row.getInt("id"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int getCount() {
return mThumbs.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public void setThumbWidth(int width) {
thumbWidth = width;
}
public List<String> getViews() {
return mViews;
}
public View getView(final int position, final View convertView, ViewGroup parent) {
final PycsellImageView imageView;
String imageUrlDirty = mThumbs.get(position);
String imageUrlClean = imageUrlDirty.split("\\?")[0];
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new PycsellImageView(mContext, mIds.get(position));
imageView.setLayoutParams(new GridView.LayoutParams(thumbWidth+3, thumbWidth+3));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
//imageView.setVisibility(View.INVISIBLE);
} else {
imageView = (PycsellImageView) convertView;
imageView.setPicId(mIds.get(position));
if(imageView.getCurrentDrawable() != mThumbs.get(position) || isFlinging == true) {
imageView.setImageDrawable(null);
imageView.setCurrentDrawable("");
}
// imageView.setVisibility(View.INVISIBLE);
}
if(imageData.get(position) != null) {
if (imageView.getDrawable() == null) {
imageView.startFade();
}
imageView.setImageBitmap(imageData.get(position));
imageView.setCurrentDrawable(mThumbs.get(position));
}
else if (isFlinging == false) {
//Log.d("Picture Adapter", "Getting album thumb: "+imageUrlClean);
DownloadHelper downloadHelper = new DownloadHelper() {
public void OnFailure(String response) {
Log.e("Picture Adapter", response);
}
public void OnSucess(Bitmap bitmap) {
if (imageView.getDrawable() == null) {
imageView.setImageBitmap(bitmap);
imageView.setCurrentDrawable(mThumbs.get(position));
imageView.startFade();
}
imageData.put(position, bitmap);
}
};
new ImageTask(mContext, downloadHelper, imageUrlClean).execute();
}
return imageView;
}
}
Most of the code in the adapter isn't relevant for this, but I present it in its entirety. You'll notice that the request image is downloaded via a DownloadHelper AsyncTask, and is placed in a local SparseArray. If the image has to be shown again, it will be fetched from this array rather than being re-downloaded.
Obviously, this is very bad, as there can be a big number of images. It's just a placeholder solution. I obviously want to implement a more robust image caching, as these images have to be available offline once downloaded as well. The thing is... I don't know how.
I've implemented a Database...
public class PycsellDatabase extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String DB_NAME = "pycsell_data";
public static final String TABLE_ALBUMS_IMAGES = "albums_and_images";
public static final String ID="_id";
public static final String COL_TYPE="type";
public static final String COL_PYCID="pycsell_id";
public static final String COL_THUMB="thumb";
public static final String COL_VIEW="view";
public static final String COL_ALBUM="album";
public static final String COL_TITLE="title";
public static final String COL_PICNUM="picnum";
private static final String CREATE_TABLE_ALBUMS_IMAGES = "create table" + TABLE_ALBUMS_IMAGES + "("
+ ID + "integer primary key autoincrement"
+ COL_TYPE + "integer not null"
+ COL_PYCID + "integer not null"
+ COL_THUMB + "text not null"
+ COL_VIEW + "text"
+ COL_ALBUM +"integer"
+ COL_TITLE + "text"
+ COL_PICNUM + "integer);";
private static final String DB_SCHEMA = CREATE_TABLE_ALBUMS_IMAGES;
public PycsellDatabase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_SCHEMA);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ALBUMS_IMAGES);
onCreate(db);
}
}
...and (partially) a ContentProvider...
public class BitmapProvider extends ContentProvider {
private PycsellDatabase pDB;
private static final String AUTHORITY = "com.atlantbh.pycsell.db.BitmapProvider";
public static final int ALBUMS=100;
public static final int IMAGES=110;
public static final int SINGLE_IMAGE=120;
private static final String ALBUMS_IMAGES_BASE_PATH = "albums_images";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + ALBUMS_IMAGES_BASE_PATH);
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE+"/album_image";
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE+"/album_image";
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, ALBUMS_IMAGES_BASE_PATH, ALBUMS);
sURIMatcher.addURI(AUTHORITY, ALBUMS_IMAGES_BASE_PATH + "/#", IMAGES);
sURIMatcher.addURI(AUTHORITY, ALBUMS_IMAGES_BASE_PATH + "/#/#", SINGLE_IMAGE);
}
@Override
public boolean onCreate() {
pDB = new PycsellDatabase(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(PycsellDatabase.TABLE_ALBUMS_IMAGES);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
//1 = albums, 2 = images
case ALBUMS:
queryBuilder.appendWhere(PycsellDatabase.COL_TYPE+"= 1");
break;
case IMAGES:
queryBuilder.appendWhere(PycsellDatabase.COL_TYPE+"= 2");
queryBuilder.appendWhere(PycsellDatabase.COL_ALBUM+"="+uri.getLastPathSegment());
break;
case SINGLE_IMAGE:
List<String> segments = uri.getPathSegments();
queryBuilder.appendWhere(PycsellDatabase.COL_TYPE+"= 2");
queryBuilder.appendWhere(PycsellDatabase.COL_ALBUM+"="+segments.get(0));
queryBuilder.appendWhere(PycsellDatabase.COL_PYCID+"="+segments.get(1));
break;
}
Cursor cursor = queryBuilder.query(pDB.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
and I am fairly certain I can handle these myself as I go... what I have a problem with is the actual adapter. I am aware of how to implement a cursor adapter, but with that I can only check against the DB, rather than perform the "download first, fetch from DB later". I imagine the logic would be something like this:
- Query for an image via the provider
- Pass the returning cursor to the adapter
- If the cursor is empty (i.e. no entry in the DB for that image), download it and put it into the DB
However I am not 100% certain this is the way to do it. Any help would be greatly appreciated.
Best regards, Damir H.