android-ibc-forum

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit bb076782b738a7db295dbe72378f1a3d52e88e77
parent 79455df70ff09eae4e3782173446ed148655eb7c
Author: Jan Dankert <devnull@localhost>
Date:   Sat, 11 Feb 2012 00:04:50 +0100

Sehr einfacher Bilder-Cache, der aber möglicherweise die Ladezeit der Activity überlebt.

Diffstat:
src/de/mtbnews/android/image/URLImageParser.java | 31+++++++++++++++++++++++--------
src/org/xmlrpc/android/XMLRPCClient.java | 6------
src/org/xmlrpc/android/XMLRPCSerializer.java | 2+-
3 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/src/de/mtbnews/android/image/URLImageParser.java b/src/de/mtbnews/android/image/URLImageParser.java @@ -3,12 +3,13 @@ package de.mtbnews.android.image; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.util.Map; +import java.util.WeakHashMap; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; -import android.R.drawable; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.AsyncTask; @@ -19,8 +20,14 @@ import de.mtbnews.android.util.IBC; public class URLImageParser implements ImageGetter { - Context c; - View container; + /** + * Einfacher Bildercache, der zu einer URL das {@link Drawable} speichert. + * Achtung: Bei nächsten GC-Lauf wird der Inhalt der Map entfernt. + */ + private static Map<String, Drawable> drawableCache = new WeakHashMap<String, Drawable>(); + + private Context context; + private View view; /*** * Construct the URLImageParser which will execute AsyncTask and refresh the @@ -31,8 +38,8 @@ public class URLImageParser implements ImageGetter */ public URLImageParser(View t, Context c) { - this.c = c; - this.container = t; + this.context = c; + this.view = t; } public Drawable getDrawable(String source) @@ -81,7 +88,7 @@ public class URLImageParser implements ImageGetter urlDrawable.drawable = result; // redraw the image by invalidating the container - URLImageParser.this.container.invalidate(); + URLImageParser.this.view.invalidate(); } } @@ -91,10 +98,15 @@ public class URLImageParser implements ImageGetter * @param urlString * @return */ - public Drawable fetchDrawable(String urlString) + private Drawable fetchDrawable(String urlString) { try { + // Bild bereits im Cache? + final Drawable drawableFromCache = drawableCache.get(urlString); + if (drawableFromCache != null) + return drawableFromCache; + InputStream is = fetch(urlString); Drawable drawable = Drawable.createFromStream(is, "src"); if (drawable == null) @@ -102,7 +114,10 @@ public class URLImageParser implements ImageGetter else drawable.setBounds(0, 0, 0 + drawable.getIntrinsicWidth(), 0 + drawable.getIntrinsicHeight()); - return drawable; + + // Bild in den Cache einfügen + drawableCache.put(urlString, drawable); + return drawable; } catch (Exception e) { diff --git a/src/org/xmlrpc/android/XMLRPCClient.java b/src/org/xmlrpc/android/XMLRPCClient.java @@ -140,12 +140,6 @@ public class XMLRPCClient extends XMLRPCCommon public CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); - @Override - protected void finalize() throws Throwable - { - Log.w(IBC.TAG,"*********** destructing the XMLRPC client ************"); - super.finalize(); - } /** * XMLRPCClient constructor. Creates new instance based on server URI (Code * contributed by sgayda2 from issue #17, and by erickok from ticket #10) diff --git a/src/org/xmlrpc/android/XMLRPCSerializer.java b/src/org/xmlrpc/android/XMLRPCSerializer.java @@ -163,7 +163,7 @@ class XMLRPCSerializer implements IXMLRPCSerializer { } else if (typeNodeName.equals(TYPE_BASE64)) { String value = parser.nextText(); - BufferedReader reader = new BufferedReader(new StringReader(value),250); + BufferedReader reader = new BufferedReader(new StringReader(value),50); String line; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) {