I've taken a piece out of some of the major caching/image loading methods people have made and came up with something that uses the browsers cache and loads the images very fast.
My problem is that sometimes the images are not correct(duplicate of another image) and when I scroll down the list of imageViews the images flicker and change as scrolling. Is this something I have to live with?
Yes I create a new thread for each imageView. I tried one thread in a queue and it took too long to load. Even though they are all separate threads it should not get the imageView's loaded with the wrong drawables.
Here is my code.
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
if (imageView != null && urlString != null && urlString.length() > 0)
{
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageView.setImageDrawable((Drawable) message.obj);
}
};
Thread thread = new Thread() {
@Override
public void run() {
if (imageView != null && urlString != null && !urlString.equals(""))
{
URL url;
try {
InputStream is = fetch(urlString);
if (is != null) {
Drawable drawable = Drawable.createFromStream((InputStream)is, "src");
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
}
catch (MalformedURLException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
}
}
}
};
thread.start();
}
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
if (urlString != null)
{
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof InputStream) {
return (InputStream) response;
}
}
return null;
}
*EDIT***
After all the comments here is my code: I'm currently getting a 05-19 00:16:49.535: ERROR/AndroidRuntime(12213): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4070dab0 when I try to recycle.
Also, when I didnt have the recycle everything works except I see the images still flickering. And even sometimes the wrong image is displayed.
public void fetchDrawableOnThread(final String urlString, final ImageView imageView) {
if (imageView != null && urlString != null && urlString.length() > 0)
{
imageView.setTag(urlString);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
Drawable d = imageView.getDrawable();
if(d != null){
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
imageView.setImageBitmap(null);
}
imageView.setImageDrawable((Drawable) message.obj);
}
};
Thread thread = new Thread() {
@Override
public void run() {
if (imageView != null && urlString != null && !urlString.equals(""))
{
URL url;
try {
if (imageView.getTag().equals(urlString))
{
InputStream is = fetch(urlString);
if (is != null) {
Drawable drawable = Drawable.createFromStream((InputStream)is, "src");
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
}
}
catch (MalformedURLException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
} catch (IOException e) {
Log.e(this.getClass().getSimpleName(), "fetchDrawable failed", e);
}
}
}
};
thread.start();
}
}
private InputStream fetch(String urlString) throws MalformedURLException, IOException {
if (urlString != null)
{
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (response instanceof InputStream) {
return (InputStream) response;
}
}
return null;
}