android - how to cache an image from a remote site

Posted by Lynnooi on Stack Overflow See other posts from Stack Overflow or by Lynnooi
Published on 2010-06-11T06:59:36Z Indexed on 2010/06/11 7:02 UTC
Read the original article Hit count: 194

Filed under:

Hi,

Can anyone please provide me some example on how to save an image i fetch from websites into a cache. I had try to include the following function into my code and call it once i run the activity.

public void getRemoteImage(String imageUrl) {
    imageUrl = "http://marga.mobile9.com/download/thumb/295/sexylady7_xo6npovn.jpg";
      URL aURL = null;
      URLConnection conn = null;
      Bitmap bmp = null;
      CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap());
      if (cache_result == null) {
        try {
            aURL = new URL(imageUrl);
            conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            cache_result = new CacheManager.CacheResult();
            CacheManager.saveCacheFile(imageUrl, cache_result);
        } catch (Exception e) {
            //return null;
        }
    }
    bmp = BitmapFactory.decodeStream(cache_result.getInputStream());*/
    Toast.makeText(context,"Please work.. namo namo namo", Toast.LENGTH_SHORT).show();
    //return bmp;
}

However, I got a nullPointerException. Can someone please help me with it as i'm quite new in android.

© Stack Overflow or respective owner

Related posts about cache