Saturday, September 22, 2012

Utility Calss - ImageMangerHelper

The Following Code snippets used to fetching the image from targeted image URL

/**
 * ImageManager is used to fetch the image from the targeted url. It is a
 * immutable pattern.
 *
 * @author ganesan_sh
 *
 */
public final class ImageManager {

    static ImageManager imageManager = null;
    private static long updatedTime = 0;
    private static long resetPeriod = 4320000;
    private static Bitmap bmImg;

    private ImageManager() {
    }

    public static ImageManager getImageManagerInstance(String imageURL) {
    long currTime = System.currentTimeMillis();
    if (((currTime - updatedTime) > resetPeriod) || imageManager == null) {
        imageManager = new ImageManager();
        fetchAdvBannerImage(imageURL);
    }
    return imageManager;
    }

    static void fetchAdvBannerImage(String fileUrl) {
    URL myFileUrl = null;
    try {
        myFileUrl = new URL(fileUrl);
    } catch (MalformedURLException e) {
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl
            .openConnection();
        conn.setDoInput(true);
        conn.connect();
        int length = conn.getContentLength();
        InputStream is = conn.getInputStream();
        bmImg = BitmapFactory.decodeStream(is);

    } catch (IOException e) {
    }
    }

    public Bitmap getBannerImage() {
    return bmImg;
    }
}