android 文件MD5加密 对SD卡的操作

  File filePath;
   if (Environment.getExternalStorageState().equals(
     Environment.MEDIA_MOUNTED))// 判断是否插入SD卡
   {    filePath = Environment.getExternalStorageDirectory(); // 获得sdcard的路径
    StatFs stat = new StatFs(filePath.getPath()); // 创建StatFs对象,这个对象很重要SD卡的信息就靠它获取了
    long blockSize = stat.getBlockSize(); // 获得block的大小
    float totalBlocks = stat.getBlockCount(); // 获得总容量
    int sizeInMb = (int) (blockSize * totalBlocks) / 1024 / 1024; // 转换成单位是兆的
    long availableBlocks = stat.getAvailableBlocks(); // 获得可用容量
    float percent = availableBlocks / totalBlocks; // 获得可用比例
    percent = (int) (percent * 1000); // 舍去多余小数位数
    FileByMD5 m_md5 = new FileByMD5();
    String request = m_md5.md5sum(fDir + "/xxxx.xml");
   }
public class FileByMD5 {  private char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7','8', '9',
    'A', 'B', 'C', 'D', 'E', 'F' };  public String toHexString(byte[] b) 
  {
   StringBuilder sb = new StringBuilder(b.length * 2);
   for (int i = 0; i < b.length; i++) 
   {
    sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
    sb.append(HEX_DIGITS[b[i] & 0x0f]);
   }
   return sb.toString();
  }  public String md5sum(String filename) 
  {
   InputStream fis;
   byte[] buffer = new byte[1024];
   int numRead = 0;
   MessageDigest md5;
   try 
   {
    fis = new FileInputStream(filename);
    md5 = MessageDigest.getInstance("MD5");
    while ((numRead = fis.read(buffer)) > 0) 
    {
     md5.update(buffer, 0, numRead);
    }
    fis.close();
    return toHexString(md5.digest());   } 
   catch (Exception e)
   {
    System.out.println("error");
    return null;
   }
  }
 } nbsp;

你可能感兴趣的:(android 文件MD5加密 对SD卡的操作)