public Avatar getPepAvatar(Uri image, int size, Bitmap.CompressFormat format) { try { Avatar avatar = new Avatar(); Bitmap bm = cropCenterSquare(image, size); if (bm == null) { return null; } ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream(); Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT); MessageDigest digest = MessageDigest.getInstance("SHA-1"); DigestOutputStream mDigestOutputStream = new DigestOutputStream(mBase64OutputStream, digest); if (!bm.compress(format, 75, mDigestOutputStream)) { return null; } mDigestOutputStream.flush(); mDigestOutputStream.close(); avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest()); avatar.image = new String(mByteArrayOutputStream.toByteArray()); return avatar; } catch (NoSuchAlgorithmException e) { return null; } catch (IOException e) { return null; } }
public Avatar getStoredPepAvatar(String hash) { if (hash == null) { return null; } Avatar avatar = new Avatar(); File file = new File(getAvatarPath(hash)); FileInputStream is = null; try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); is = new FileInputStream(file); ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream(); Base64OutputStream mBase64OutputStream = new Base64OutputStream(mByteArrayOutputStream, Base64.DEFAULT); MessageDigest digest = MessageDigest.getInstance("SHA-1"); DigestOutputStream os = new DigestOutputStream(mBase64OutputStream, digest); byte[] buffer = new byte[4096]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } os.flush(); os.close(); avatar.sha1sum = CryptoHelper.bytesToHex(digest.digest()); avatar.image = new String(mByteArrayOutputStream.toByteArray()); avatar.height = options.outHeight; avatar.width = options.outWidth; return avatar; } catch (IOException e) { return null; } catch (NoSuchAlgorithmException e) { return null; } finally { close(is); } }