How to get md5 of multiple images in short time?

219 Views Asked by At

In my app, i am generating md5 of about 3000 images having size up to 700 MB. I already have Arraylist of file paths of images.

The problem is time consumption which is up to 55 seconds. I want to reduce it up to least time, taken up to 8 to 12 seconds at least if possible or even less.

This is the algorithm for generating 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 is taking String as key, value as Arraylist

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

This is algorithm for finding same md5 duplicate files

 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);
     }

    }
}

calling Duplicate finder in Async task,s do in background and showing loading animation to user while all md5 of files are taken and displayed in recycler view.

0

There are 0 best solutions below