工具+方法

检查图片是否损坏
/**
 * 检查图片是否损坏
 *
 * @param filePath
 * @return
 */
public static boolean checkImgDamage(String filePath) {
    BitmapFactory.Options options = null;
    if (options == null) {
        options = new BitmapFactory.Options();
    }
    options.inJustDecodeBounds = true;
        
    BitmapFactory.decodeFile(filePath, options);
    if (options.mCancel || options.outWidth == -1
                || options.outHeight == -1) {
        return true;
    }
    return false;
}

Android 根据包名获取SHA1和MD5

//这个是获取SHA1的方法
   public static String getCertificateSHA1Fingerprint(Context context,String packageName) {
       //获取包管理器
       PackageManager pm = context.getPackageManager();
       //获取当前要获取SHA1值的包名,也可以用其他的包名,但需要注意,
       //在用其他包名的前提是,此方法传递的参数Context应该是对应包的上下文。
       //返回包括在包中的签名信息
       int flags = PackageManager.GET_SIGNATURES;
       PackageInfo packageInfo = null;
       try {
           //获得包的所有内容信息类
           packageInfo = pm.getPackageInfo(packageName, flags);
       } catch (PackageManager.NameNotFoundException e) {
           e.printStackTrace();
       }
       //签名信息
       Signature[] signatures = packageInfo.signatures;
       byte[] cert = signatures[0].toByteArray();
       //将签名转换为字节数组流
       InputStream input = new ByteArrayInputStream(cert);
       //证书工厂类,这个类实现了出厂合格证算法的功能
       CertificateFactory cf = null;
       try {
           cf = CertificateFactory.getInstance("X509");
       } catch (CertificateException e) {
           e.printStackTrace();
       }
       //X509证书,X.509是一种非常通用的证书格式
       X509Certificate c = null;
       try {
           c = (X509Certificate) cf.generateCertificate(input);
       } catch (CertificateException e) {
           e.printStackTrace();
       }
       String hexString = null;
       try {
           //加密算法的类,这里的参数可以使MD4,MD5等加密算法
           MessageDigest md = MessageDigest.getInstance("SHA1");
           //获得公钥
           byte[] publicKey = md.digest(c.getEncoded());
           //字节到十六进制的格式转换
           hexString = byte2HexFormatted(publicKey);
       } catch (NoSuchAlgorithmException e1) {
           e1.printStackTrace();
       } catch (CertificateEncodingException e) {
           e.printStackTrace();
       }
       return hexString;
   }

   //这里是将获取到得编码进行16进制转换
   private static String byte2HexFormatted(byte[] arr) {
       StringBuilder str = new StringBuilder(arr.length * 2);
       for (int i = 0; i < arr.length; i++) {
           String h = Integer.toHexString(arr[i]);
           int l = h.length();
           if (l == 1)
               h = "0" + h;
           if (l > 2)
               h = h.substring(l - 2, l);
           str.append(h.toUpperCase());
           if (i < (arr.length - 1))
               str.append(':');
       }
       return str.toString();
   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值