I’m using AQuery to help cache some images. I know for fact the images are being cached correctly, but I’m having trouble accessing them unless I’m online.
I know they are being cached because I can see them on my SD card and if I replace mArtistImageURL in my return statement with the URL from my browser (copy/paste), the image will load while I’m offline, if it’s already cached. I realize that I’m requesting to fetch the image URLs only if I’m online, but even so, I don’t understand why the cached images won’t load offline.
MusicUtils.isOnline() is basically a check to see if there is any connection. The main reason I have the bulk of my doInBackground() method wrapped in this is do to an Exception being thrown if I don’t. FATAL EXCEPTION: AsyncTask #1 is all Logcat is putting out for me at the moment. I’m not sure why the stacktrace isn’t complete.
I’ve tried using my AsyncTask in every way I know. I’ve returned a Bitmap, File, and String and I’m able to successfully load the image in each case, as long as I’m online. So, I have these cached images and I need some help putting them to use.
AsyncTask
public class loadArtistImage extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... arg0) {
// Get artist image
if (MusicUtils.isOnline(mContext)) {
mArtistResults = Artist.getImages("Andrew Bird", 1, 1, key);
mArtistIterator = mArtistResults.getPageResults().iterator();
while (mArtistIterator.hasNext()) {
mArtistImageURL = mArtistIterator.next().getImageURL(
ImageSize.ORIGINAL);
}
}
aq.cache(mArtistImageURL, 60000 * 1440);
return aq.getCachedImage(mArtistImageURL);
}
@Override
protected void onPostExecute(Bitmap result) {
bm = result;
if (bm!= null) {
mArtistImage.postDelayed(new Runnable() {
@Override
public void run() {
mArtistImage.startAnimation(AnimationUtils
.loadAnimation(mContext, android.R.anim.fade_in));
MusicUtils.setArtistBackground(mArtistImage, bm);
}
}, 666);
} else {
// TODO something
}
super.onPostExecute(result);
}
}
Mm. While offline what the value of mArtistImageURL will be? Null? So at this point
aq.cache(mArtistImageURL, 60000 * 1440);you are caching null, and at next line you are trying to get cache for null? Am I right?Update: I suppose you cache images using url hash value for file name or something like this. So, modify your cache method to accept additional parameter for filename, for example
aq.cache(mArtistImageURL,mArtistNameWithoutSpaces, 60000 * 1440);and when you want a file offline just callaq.getCachedImage(mArtistNameWithoutSpaces);so it will look at your cache folder and return a right file, or stub image if no file is found.Update 2: Okay, if you dont want to modify AQuery cache methods you can try (againg) using shared preferences (so they would work) like this (not tested):
So in the end your code will look something like this:
Oh, yeah, I dont know how AQuery behaves, but anyway you probably should only cache images while online.