Check if a file is an image or a video

If you are getting your Uri from a content resolver you can get the mime type using getType(Uri);

ContentResolver cR = context.getContentResolver();
String type = cR.getType(uri); 

should get back something similar to "image/jpeg" which you can check against for your display logic.


I'd check the mimeType and then check if it corresponds to an image or video.

A full example, for checking if a filepath is an image, would be:

public static boolean isImageFile(String path) {
    String mimeType = URLConnection.guessContentTypeFromName(path);
    return mimeType != null && mimeType.startsWith("image");
}

And for video:

public static boolean isVideoFile(String path) {
    String mimeType = URLConnection.guessContentTypeFromName(path);
    return mimeType != null && mimeType.startsWith("video");
}

Tags:

Android