Ejemplo n.º 1
0
        public Bitmap GetBitmap(string url)
        {
            Java.IO.File file = fileCache.GetFile(url);
            //先从文件缓存中查找是否有
            //from SD cache
            Bitmap b = DecodeFile(file);

            if (b != null)
            {
                return(b);
            }

            /**
             *  最后从指定的url中下载图片
             */
            //from web
            try {
                Bitmap            bitmap   = null;
                URL               imageUrl = new URL(url);
                HttpURLConnection conn     = (HttpURLConnection)imageUrl.OpenConnection();
                conn.Connect();
                conn.ConnectTimeout          = 30000;
                conn.ReadTimeout             = 30000;
                conn.InstanceFollowRedirects = true;
                var inStream     = conn.InputStream;
                var outputStream = new FileOutputStream(file);
                CopyStream(inStream, outputStream);
                outputStream.Close();
                conn.Disconnect();
                bitmap = DecodeFile(file);

                return(bitmap);
            } catch (Throwable ex) {
                ex.PrintStackTrace();
                if (ex is OutOfMemoryError)
                {
                    memoryCache.Clear();
                }
                return(null);
            }
        }