Code example for URL

Methods: openConnection

0
        private Bitmap DownloadBitmapFromWeb(URL url, File f, Boolean isRetry)
        { 
            try { 
                Bitmap bitmap = null;
                final HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection(mProxy);
                if (mIgnoreSSLError) {
                    Util.setIgnoreSSLError(conn);
                } 
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                conn.setInstanceFollowRedirects(true);
                final InputStream is = conn.getInputStream();
                final OutputStream os = new FileOutputStream(f);
                copyStream(is, os);
                os.close();
                bitmap = decodeFile(f);
 
                int bitmapBytes = bitmap.getByteCount();
                if (bitmapBytes == 0){
                    if (isRetry)
                    { 
                        return null; 
                    } 
                    else 
                    { 
                        return DownloadBitmapFromWeb(url, f, true);
                    } 
                } 
                mFileCache.saveFile(bitmap, url);
                return bitmap;
            } catch (final FileNotFoundException e) {
                // Storage state may changed, so call FileCache.init() again. 
                // e.printStackTrace(); 
                mFileCache.init();
            } catch (final IOException e) {
                // e.printStackTrace(); 
            } 
            return null; 
        } 
 
        @Override 
        public void run() { 
            final Bitmap bmp = getBitmap(mImageToLoad.source);
            if (imageViewReused(mImageToLoad) || mImageToLoad.source == null)
                return; 
            mMemoryCache.put(mImageToLoad.source, bmp);
            if (imageViewReused(mImageToLoad)) return;
            final BitmapDisplayer bd = new BitmapDisplayer(bmp, mImageToLoad);
            final Activity a = (Activity) mImageToLoad.imageview.getContext();
            a.runOnUiThread(bd);
        } 
    } 
 
    private static class ImageToLoad { 
 
        public final URL source;
        public final ImageView imageview;
 
        public ImageToLoad(final URL source, final ImageView imageview) {
            this.source = source;
            this.imageview = imageview;
        } 
    } 
 
    public static class ExpiringBitmap { 
 
        public final Bitmap image;
        public final Date expires;
 
        public ExpiringBitmap(final Bitmap image, final Date expires) {
            this.image = image;
            this.expires = expires;
        } 
    } 
 
    private static class MemoryCache { 
 
        private final int mMaxCapacity;
        private final Map<URL, SoftReference<ExpiringBitmap>> mSoftCache;
        private final Map<URL, ExpiringBitmap> mHardCache;
 
        public MemoryCache(int max_capacity) {
            mMaxCapacity = max_capacity;
            mSoftCache = new ConcurrentHashMap<URL, SoftReference<ExpiringBitmap>>();
            mHardCache = new LinkedHashMap<URL, ExpiringBitmap>(mMaxCapacity / 3,
                    0.75f, true) { 
 
                private static final long serialVersionUID = 1347795807259717646L;
 
                @Override 
                protected boolean removeEldestEntry( 
                        LinkedHashMap.Entry<URL, ExpiringBitmap> eldest) {
                    // Moves the last used item in the hard cache to the soft 
                    // cache. 
                    if (size() > mMaxCapacity) {
                        mSoftCache.put(eldest.getKey(),
                                new SoftReference<ExpiringBitmap>(eldest.getValue()));
                        return true; 
                    } else 
                        return false; 
                } 
            }; 
        } 
 
        public Set<URL> clear() {
            Set<URL> clearedUrls = new HashSet<URL>();
 
            synchronized (mHardCache) {
 
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, -1);
                Map<URL, ExpiringBitmap> copy = new HashMap<URL, ExpiringBitmap>(mHardCache);
                for (URL url : copy.keySet()){
 
                    ExpiringBitmap bitmap = mHardCache.get(url);
                    if (bitmap != null) {
                        if (bitmap.expires.before(cal.getTime())) {
                            clearedUrls.add(url);
                        } 
                    } 
                } 
                for (URL url : clearedUrls){
                    mHardCache.remove(url);
                    mSoftCache.remove(url);
                } 
            } 
 
            return clearedUrls;
        } 
 
        public Set<URL> getActiveUrls(){
            return new HashSet<URL>(mHardCache.keySet());
        } 
 
        public Bitmap get(final URL url, final FileCache fileCache) {
            synchronized (mHardCache) {
                ExpiringBitmap bitmap = mHardCache.get(url);
                if (bitmap != null) {
                    if (bitmap.expires.before(new Date())) {
                        mHardCache.remove(url);
                        fileCache.deleteFile(url);
                        bitmap = null;
                    } else { 
                        // Put bitmap on top of cache so it's purged last. 
                        try { 
                            mHardCache.remove(url);
                            mHardCache.put(url, bitmap);
                        } catch (Exception e) {
 
                        } 
                        return bitmap.image;
                    } 
                } 
            } 
 
            final SoftReference<ExpiringBitmap> bitmapRef = mSoftCache.get(url);
            if (bitmapRef != null) {
                final ExpiringBitmap bitmap = bitmapRef.get();
                if (bitmap != null)
                    return bitmap.image;
                else { 
                    // Must have been collected by the Garbage Collector 
                    // so we remove the bucket from the cache. 
                    mSoftCache.remove(url);
                } 
            } 
 
            // Could not locate the bitmap in any of the caches, so we return 
            // null. 
            return null; 
 
        } 
 
        public void put(final URL url, final Bitmap bitmap) {
            if (url == null || bitmap == null) return;
 
            if (mHardCache.get(url) == null) {
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.DATE, +1);
                mHardCache.put(url, new ExpiringBitmap(bitmap, cal.getTime()));
            } 
        } 
    } 
 
}