示例#1
0
  /**
   * Decode and return the bitmap represented by the given index in the Icon Directory, if valid.
   *
   * @param index The index into the Icon Directory of the image of interest.
   * @return The decoded Bitmap object for this image, or null if the entry is invalid or decoding
   *     fails.
   */
  public Bitmap decodeBitmapAtIndex(int index) {
    final IconDirectoryEntry iconDirEntry = iconDirectory[index];

    if (iconDirEntry.payloadIsPNG) {
      // PNG payload. Simply extract it and decode it.
      return BitmapUtils.decodeByteArray(
          decodand, offset + iconDirEntry.payloadOffset, iconDirEntry.payloadSize);
    }

    // The payload is a BMP, so we need to do some magic to get the decoder to do what we want.
    // We construct an ICO containing just the image we want, and let Android do the rest.
    byte[] decodeTarget =
        new byte
            [ICO_HEADER_LENGTH_BYTES + ICO_ICONDIRENTRY_LENGTH_BYTES + iconDirEntry.payloadSize];

    // Set the type field in the ICO header.
    decodeTarget[2] = 1;

    // Set the num-images field in the header to 1.
    decodeTarget[4] = 1;

    // Copy the ICONDIRENTRY we need into the new buffer.
    System.arraycopy(
        decodand,
        offset + iconDirEntry.getOffset(),
        decodeTarget,
        ICO_HEADER_LENGTH_BYTES,
        ICO_ICONDIRENTRY_LENGTH_BYTES);

    // Copy the payload into the new buffer.
    final int singlePayloadOffset = ICO_HEADER_LENGTH_BYTES + ICO_ICONDIRENTRY_LENGTH_BYTES;
    System.arraycopy(
        decodand,
        offset + iconDirEntry.payloadOffset,
        decodeTarget,
        singlePayloadOffset,
        iconDirEntry.payloadSize);

    // Update the offset field of the ICONDIRENTRY to make the new ICO valid.
    decodeTarget[ICO_HEADER_LENGTH_BYTES + 12] = (byte) singlePayloadOffset;
    decodeTarget[ICO_HEADER_LENGTH_BYTES + 13] = (byte) (singlePayloadOffset >>> 8);
    decodeTarget[ICO_HEADER_LENGTH_BYTES + 14] = (byte) (singlePayloadOffset >>> 16);
    decodeTarget[ICO_HEADER_LENGTH_BYTES + 15] = (byte) (singlePayloadOffset >>> 24);

    // Decode the newly-constructed singleton-ICO.
    return BitmapUtils.decodeByteArray(decodeTarget);
  }
  private static void storeFaviconsInMemCache(Cursor c) {
    if (c == null || !c.moveToFirst()) {
      return;
    }

    final Favicons favicons = Favicons.getInstance();

    do {
      final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
      final byte[] b = c.getBlob(c.getColumnIndexOrThrow(URLColumns.FAVICON));

      if (b == null) {
        continue;
      }

      Bitmap favicon = BitmapUtils.decodeByteArray(b);
      if (favicon == null) {
        continue;
      }

      favicon = favicons.scaleImage(favicon);
      favicons.putFaviconInMemCache(url, favicon);
    } while (c.moveToNext());
  }