1

在我的应用程序中,我生成了大约 3000 张图像的 md5,大小最大为 700 MB。我已经有图像文件路径的 Arraylist。

问题是时间消耗高达 55 秒。我想将它减少到最少的时间,如果可能的话至少需要 8 到 12 秒甚至更少。

这是生成md5的算法

  public static String getMd5OfFile(String filePath)
{
    String returnVal = "";
    try
    {
        InputStream input   = new FileInputStream(filePath);
        byte[] buffer = new byte[15360];     
      //byte[] buffer = new byte[1024];
      // byte[] buffer = new byte[8192];
        MessageDigest md5Hash = MessageDigest.getInstance("MD5");
        int           numRead = 0;
        while (numRead != -1)
        {
            numRead = input.read(buffer);
            if (numRead > 0)
            {
                md5Hash.update(buffer, 0, numRead);
            }
        }
        input.close();

        byte [] md5Bytes = md5Hash.digest();
        for (int i=0; i < md5Bytes.length; i++)
        {
            returnVal += Integer.toString( ( md5Bytes[i] & 0xff ) + 0x100, 16).substring( 1 );
        }
    }
    catch(Throwable t) {t.printStackTrace();}
    return returnVal.toUpperCase();
}

map 以 String 为 key,value 为 Arraylist

HashMap<String,Arraylist<String>> map = new HashMap<String, String>();

这是查找相同 md5 重复文件的算法

 void Duplicatefinder(ArrayList<String> filepaths)
{

    HashMap<String,String> checkmap = new HashMap<String, String>();
    for (String filepath : filepaths)
    {

        String md5 = getMd5OfFile(filepath);
     if(checkmap.containsKey(md5)) {
         if (!map.containsKey(md5)) {
             map.put(md5, new ArrayList<String>());
             String original = checkmap.get(md5);
             ArrayList<String> list = map.get(md5);
             list.add(original);
             map.put(md5,list);
         }
         ArrayList<String> list = map.get(md5);
         list.add(filepath);
         map.put(md5, list);
     }
     else {
         checkmap.put(md5,filepath);
     }

    }
}

在异步任务中调用 Duplicate finder,在后台执行并向用户显示加载动画,同时获取所有 md5 文件并在回收器视图中显示。

4

0 回答 0