Wednesday, April 16, 2014

How to check whether current file is audio/video/image?

The below method will return whether current file is audio file or not:-

public static boolean isMusic(File file) {
Uri uri = Uri.fromFile(file);
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(uri.toString()));

if (type == null)
return false;
else
return (type.toLowerCase().startsWith("audio/"));

}

The below method will return whether current file is video file or not:-

public static boolean isVideo(File file) {
Uri uri = Uri.fromFile(file);
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(uri.toString()));

if (type == null)
return false;
else
return (type.toLowerCase().startsWith("video/"));
}

The below method will return whether current file is image file or not:-

public static boolean isImage(File file) {
Uri uri = Uri.fromFile(file);
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
MimeTypeMap.getFileExtensionFromUrl(uri.toString()));

if (type == null)
return false;
else
return (type.toLowerCase().startsWith("image/"));
}