The example in this article describes how Java in Android obtains file types based on file headers. Share to everyone for your reference, specific as follows:
We've talked about the MediaFile class inside the Android system to get the file type, which is largely based on the file extension, and it's not very accurate. You can view the Android system using the MediaFile class to determine the type of audio file. In fact, the best way to get a file type is to judge it based on the file header information. The following code is posted:
public class FileType {public static final hashmap<string, string> mfiletypes = new hashmap<string, String>
;();
static {//images Mfiletypes.put ("ffd8ff", "jpg");
Mfiletypes.put ("89504E47", "PNG");
Mfiletypes.put ("47494638", "gif");
Mfiletypes.put ("49492a00", "TIF");
Mfiletypes.put ("424D", "BMP"); Mfiletypes.put ("41433130", "DWG");
CAD mfiletypes.put ("38425053", "PSD"); Mfiletypes.put ("7b5c727466", "RTF");
Journal mfiletypes.put ("3c3f786d6c", "xml");
Mfiletypes.put ("68746d6c3e", "html"); Mfiletypes.put ("44656c69766572792d646174653a", "eml");
Mail Mfiletypes.put ("D0cf11e0", "Doc");
Mfiletypes.put ("5374616e64617264204a", "MDB");
Mfiletypes.put ("252150532d41646f6265", "PS");
Mfiletypes.put ("255044462d312e", "pdf");
Mfiletypes.put ("504b0304", "zip");
Mfiletypes.put ("52617221", "rar");
Mfiletypes.put ("57415645", "wav");
Mfiletypes.put ("41564920", "avi"); Mfiletypes.put ("2e524d46", "rm");
Mfiletypes.put ("000001BA", "mpg");
Mfiletypes.put ("000001b3", "mpg");
Mfiletypes.put ("6d6f6f76", "mov");
Mfiletypes.put ("3026b2758e66cf11", "ASF");
Mfiletypes.put ("4d546864", "mid");
Mfiletypes.put ("1f8b08", "GZ");
Mfiletypes.put ("", "");
Mfiletypes.put ("", "");
public static string Getfiletype (String filePath) {return Mfiletypes.get (GetFileHeader (FilePath));
//Get file header information public static string GetFileHeader (String filePath) {fileinputstream = null;
String value = null;
try {is = new FileInputStream (FilePath);
Byte[] B = new Byte[3];
Is.read (b, 0, b.length);
Value = bytestohexstring (b);
catch (Exception e) {} finally {if (null!= is) {try {is.close ();
The catch (IOException e) {}} return value;
private static String bytestohexstring (byte[] src) {StringBuilder builder = new StringBuilder (); if (src = null | | src.length ≪= 0) {return null;
} String HV;
for (int i = 0; i < src.length i++) {HV = Integer.tohexstring (Src[i] & 0xFF). toUpperCase ();
if (Hv.length () < 2) {builder.append (0);
} builder.append (HV);
return builder.tostring (); The public static void main (string[] args) throws Exception {final String fileType = Getfiletype ("d:/apache-tomcat-6.
0.35.tar.gz ");
System.out.println (FileType);
}
}
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage
I hope this article will help you with the Android program.