views:

499

answers:

1

I'm using a thumbnail loader in my project the one mentioned below. The problem is that the it loads all the thumbnails properly except the ones who's size is of about 40K. When our back end is giving that sort of thumbnails are not generated and sometimes this eventually leads to a Crash too.

What m I supposed to do with this ?

public class ThumbnailManager  
{

private final Map<String, Bitmap> drawableMap;
public static Context context;
private Resources res;
private int thumbnail_size;

public ThumbnailManager()
{
    drawableMap = new HashMap<String, Bitmap >();   
    res = new Resources(context.getAssets(), null, null);
    thumbnail_size = res.getInteger(R.ThumbnailManager.THUMBNAIL_SIZE);
}

public Bitmap fetchBitmap(String urlString)
{
    if(drawableMap.containsKey(urlString))
    {
        return (drawableMap.get(urlString));
    }

    //Log.d(getClass().getSimpleName(), " Image URL :: "+ urlString);
    try
    {
        InputStream is = fetch(urlString);
        android.util.Log.v("ThumbnailManager", "ThumbnailManager " + urlString);
        drawableMap.put(urlString, BitmapFactory.decodeStream(is));//Bitmap.createScaledBitmap(BitmapFactory.decodeStream(is), thumbnail_size, thumbnail_size, false));
        return drawableMap.get(urlString);
    }
    catch(Exception e)
    {
        android.util.Log.v("EXCEPTION", "EXCEPTION" + urlString);
        return null;
    }
}

public void fetchBitmapOnThread(final String urlString, final ImageView imageView)
{
    if(drawableMap.containsKey(urlString))
    {
        imageView.setImageBitmap(drawableMap.get(urlString));
        return;
    }

    if(urlString.compareTo("AUDIO") == 0)
    {
        Bitmap audioThumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.timeline_audio_thumb);
        drawableMap.put(urlString, Bitmap.createScaledBitmap(audioThumb, thumbnail_size, thumbnail_size, false));
        imageView.setImageBitmap(drawableMap.get(urlString));
        return;
    }

    final Handler handler = new Handler()
    {
        public void handleMessage(Message message)
        {
            imageView.setImageBitmap((Bitmap) message.obj);
        }
    };

    Thread thread = new Thread()
    {
        public void run()
        {
            Bitmap urlBitmap = fetchBitmap(urlString);
            Message message = handler.obtainMessage(1, urlBitmap);
            handler.sendMessage(message);
        }
    };
    thread.start();
}

public InputStream fetch(String urlString) throws IOException, MalformedURLException
{
    final URL url = new URL(urlString);
    final URLConnection conn = url.openConnection();
    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setAllowUserInteraction(true);
    httpConn.setInstanceFollowRedirects(true);
    httpConn.setRequestMethod("GET");
    httpConn.connect();

    return(conn.getInputStream());
}
}
+1  A: 

It would be great if you added a stacktrace here. But I suspect its an memory issue too. If you are loading lots of bitmaps into memory, they require much more memory than the original filesize. Ex. your thumbnail that is 40k could be 400k as a bitmap, It depends on the resolution. Use ddms and watch how much free memory your app is steeling. If it get less then 2MB of working memory, the BitmapFactory is likly to make a RuntimeException when decoding your large thumb.

PHP_Jedi