Ejemplo n.º 1
0
  public Bitmap getBitmap(Context context, Map<String, String> header) {
    mContext = context;
    // Don't leak context
    if (webImageCache == null) {
      webImageCache = new WebImageCache(context);
    }

    // Try getting bitmap from cache first
    Bitmap bitmap = null;
    if (mUrl != null) {
      bitmap = webImageCache.get(mUrl);
      if (bitmap == null) {
        bitmap = getBitmapFromUrl(mUrl, header);
        if (bitmap != null) {
          webImageCache.put(mUrl, bitmap);
        }
      }
    }
    return bitmap;
  }
Ejemplo n.º 2
0
  /**
   * 从网络获取图片
   *
   * @param url 图片地址
   * @return
   */
  private Bitmap getBitmapFromUrl(String url) {
    Bitmap bitmap = null;

    try {
      HttpParams params = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT);
      HttpConnectionParams.setSoTimeout(params, READ_TIMEOUT);
      HttpClient httpClient = new DefaultHttpClient(params);

      HttpGet request = new HttpGet(String.valueOf(url));
      HttpResponse response = httpClient.execute(request);

      InputStream is = null;
      ByteArrayOutputStream baos = null;
      is = response.getEntity().getContent();
      long remoteSize = response.getEntity().getContentLength();
      totalSize = currentSize + remoteSize;
      byte buffer[] = new byte[DATA_BUFFER];
      int readSize = 0;
      baos = new ByteArrayOutputStream();
      while ((readSize = is.read(buffer)) > 0) {
        baos.write(buffer, 0, readSize);
        currentSize += readSize;
        downloadPercent = (long) (currentSize * 100 / totalSize);
        if (mDownloadListener != null) {
          mDownloadListener.updateProcess(downloadPercent);
        }
        downloadSize = currentSize;
      }
      byte[] data = baos.toByteArray();

      bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

      if (mSmartImageConfig.isAutoRotate && bitmap.getWidth() > bitmap.getHeight()) {
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        bitmap =
            Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
      }
      // 缓存图片
      if (bitmap != null) {
        webImageCache.put(url, bitmap, data);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return bitmap;
  }
Ejemplo n.º 3
0
  /** 获取图片 */
  public Bitmap getBitmap(Context context) {
    // Don't leak context
    if (webImageCache == null) {
      webImageCache = new WebImageCache(context);
    }

    // 首先从缓存中获得图片
    Bitmap bitmap = null;
    if (url != null) {
      bitmap = webImageCache.get(url);
      if (bitmap == null && mSmartImageConfig.isDownloadImage) {
        bitmap = getBitmapFromUrl(url);

      } else {
        if (bitmap != null
            && mSmartImageConfig.isAutoRotate
            && bitmap.getWidth() > bitmap.getHeight()) {
          Matrix matrix = new Matrix();
          matrix.postRotate(90);
          bitmap =
              Bitmap.createBitmap(
                  bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
          if (bitmap != null) {
            webImageCache.cacheBitmapToMemory(url, bitmap);
          }
        }
      }
    }

    if (bitmap != null && mSmartImageConfig.isRound) {
      bitmap = toGrayscale(bitmap, 10);
      return bitmap;
    } else {
      return bitmap;
    }
  }
Ejemplo n.º 4
0
 public static void removeFromCache(String url) {
   if (webImageCache != null) {
     webImageCache.remove(url);
   }
 }