this method calculate the md5-checksum of a file - Android File Input Output

Android examples for File Input Output:File Hash

Description

this method calculate the md5-checksum of a file

Demo Code

/**//  w  w w.  j a  v  a 2  s  .  c o  m
 *
 * MD5Utils.java
 * 
 * Created: Jun 15, 2011 12:22:35 AM
 * 
 * Copyright (C) 2011 Paolo Dongilli and Markus Windegger
 *
 * This file is part of SasaBus.

 * SasaBus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SasaBus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SasaBus.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

import java.security.MessageDigest;
import android.util.Log;

public class Main {
    /**
     * this method calculate the md5-checksum of a file
     * @param file is the filename for calculating the md5-checksum
     * @return the calculated md5-checksum, if an error occurs null
     */
    public static String calculateMD5(File file) {
        try {
            InputStream fin = new FileInputStream(file);
            java.security.MessageDigest md5er = MessageDigest
                    .getInstance("MD5");
            byte[] buffer = new byte[1024];
            int read;
            do {
                read = fin.read(buffer);
                if (read > 0)
                    md5er.update(buffer, 0, read);
            } while (read != -1);
            fin.close();
            byte[] digest = md5er.digest();
            if (digest == null)
                return null;
            String strDigest = "";
            for (int i = 0; i < digest.length; i++) {
                strDigest += Integer.toString((digest[i] & 0xff) + 0x100,
                        16).substring(1);
            }
            Log.i("FileRetriever", "md5: " + strDigest + " length: "
                    + strDigest.length());
            return strDigest;
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials