Android Thumbnail Loading Problem

Posted by y ramesh rao on Stack Overflow See other posts from Stack Overflow or by y ramesh rao
Published on 2010-03-19T12:42:04Z Indexed on 2010/03/27 20:43 UTC
Read the original article Hit count: 208

Filed under:
|

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());
}
}

© Stack Overflow or respective owner

Related posts about android

Related posts about thumbnails