PackageMy.bag;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.math.BigInteger;Importjava.security.MessageDigest;ImportJava.util.HashMap;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classFiledigestextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); File File=NewFile ("/mnt/sdcard/123.txt"); LOG.E ("MD5", getFileMD5 (file)); } /*** Get the MD5 value of a single file! * @paramfile *@return */ Public StaticString getFileMD5 (file file) {if(!File.isfile ()) { return NULL; } MessageDigest Digest=NULL; FileInputStream in=NULL; byteBuffer[] =New byte[1024]; intLen; Try{Digest= Messagedigest.getinstance ("MD5"); Inch=Newfileinputstream (file); while(len = in.read (buffer, 0, 1024))! =-1) {digest.update (buffer,0, Len); } in.close (); } Catch(Exception e) {e.printstacktrace (); return NULL; } BigInteger bigInt=NewBigInteger (1, Digest.digest ()); returnBigint.tostring (16); } /*** Get the MD5 value of the file in the folder * *@paramfile *@paramListchild *; True recursively files in subdirectories *@return */ Public StaticMap<string, string> getDirMD5 (file file,Booleanlistchild) { if(!file.isdirectory ()) { return NULL; } Map<string, string> map =NewHashmap<string, string>(); String MD5; File files[]=File.listfiles (); for(inti = 0; i < files.length; i++) {File F=Files[i]; if(F.isdirectory () &&listchild) {Map.putall (GetDirMD5 (f, listchild)); } Else{MD5=getFileMD5 (f); if(MD5! =NULL) {map.put (F.getpath (), MD5); } } } returnmap;}} Finally, don't forget to add permissions in manifest<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
PackageMy.bag;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.math.BigInteger;Importjava.security.MessageDigest;ImportJava.util.HashMap;ImportJava.util.Map;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classFiledigestextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); File File=NewFile ("/mnt/sdcard/123.txt"); LOG.E ("MD5", getFileMD5 (file)); } /*** Get the MD5 value of a single file! * @paramfile *@return */ Public StaticString getFileMD5 (file file) {if(!File.isfile ()) { return NULL; } MessageDigest Digest=NULL; FileInputStream in=NULL; byteBuffer[] =New byte[1024]; intLen; Try{Digest= Messagedigest.getinstance ("MD5"); Inch=Newfileinputstream (file); while(len = in.read (buffer, 0, 1024))! =-1) {digest.update (buffer,0, Len); } in.close (); } Catch(Exception e) {e.printstacktrace (); return NULL; } BigInteger bigInt=NewBigInteger (1, Digest.digest ()); returnBigint.tostring (16); } /*** Get the MD5 value of the file in the folder * *@paramfile *@paramListchild *; True recursively files in subdirectories *@return */ Public StaticMap<string, string> getDirMD5 (file file,Booleanlistchild) { if(!file.isdirectory ()) { return NULL; } Map<string, string> map =NewHashmap<string, string>(); String MD5; File files[]=File.listfiles (); for(inti = 0; i < files.length; i++) {File F=Files[i]; if(F.isdirectory () &&listchild) {Map.putall (GetDirMD5 (f, listchild)); } Else{MD5=getFileMD5 (f); if(MD5! =NULL) {map.put (F.getpath (), MD5); } } } returnmap;}} Finally, don't forget to add permissions in manifest<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>