Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file is an image or a video

Tags:

Is there a way to check if the file I'm loading as a URI is a image or a video in android? I'm trying to dynamically loaded both images and videos into fragments for a list/detail view and need to tell them apart.

like image 267
user2532233 Avatar asked Jul 12 '13 15:07

user2532233


People also ask

How can I tell if a file is a video?

get(getArguments(). getString(ARG_ITEM_ID)). get(0)); ` where info_map is a map with (String, ArrayList<String>) pairings where get(0) gives the file name and get(1) right now gets a parameter from the xml for the video type.

How can I tell if file is image or video PHP?

php $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension foreach (glob("*") as $filename) { echo finfo_file($finfo, $filename) . "\n"; } finfo_close($finfo); ?> Save this answer. Show activity on this post.

Why do you think JPEG is the most used image format?

JPEGs offer you the most flexibility with raster editing and compression making them ideal for web images that need to be downloaded quickly. You want to print photos and/or artwork. At high resolution files with low compression, JPEGs are perfect for editing and then printing.


2 Answers

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"); } 
like image 199
alfongj Avatar answered Sep 20 '22 09:09

alfongj


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.

like image 32
metalmonkeysoftware Avatar answered Sep 21 '22 09:09

metalmonkeysoftware