0

I am developing an application where I have to record audio files and upload on the server.

There is scenario where internet could be down and file is only half uploaded.

How do I compare that file with one available on the android file system so that I re-upload the file if it is not uploaded successfully ?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Paras Watts
  • 2,565
  • 4
  • 21
  • 46
  • You can benefit of HTTP for this. if you have not finished the upload, you will have an error status or timeout – crgarridos May 31 '17 at 12:16

1 Answers1

0

Well you can compare checksum with some hash function like MD5 or SHA1

For MD5

Assuming you use Java for Android function will be like this

 public static String checkSum(String path){
    String checksum = null;
    try {
        FileInputStream fis = new FileInputStream(path);
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[8192];
        int numOfBytesRead;
        while( (numOfBytesRead = fis.read(buffer)) > 0){
            md.update(buffer, 0, numOfBytesRead);
        }
        byte[] hash = md.digest();
        checksum = new BigInteger(1, hash).toString(16);
    } catch (IOException ex) {
    } catch (NoSuchAlgorithmException ex) {
    }

   return checksum;
}

For PHP its much easier, you can just use md5_file function

$md5 = md5_file($filepath);
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56