user2532233
user2532233

Reputation: 841

Check if a file is an image or a video

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.

Upvotes: 29

Views: 43568

Answers (6)

Mohammad Reza
Mohammad Reza

Reputation: 95

The best way to check is ...

Image:

private fun isImage(path: String): Boolean {
        val mimeType: String = URLConnection.guessContentTypeFromName(path)
        return mimeType.startsWith("image")
}

Video:

private fun isVideo(path: String): Boolean {
        val mimeType: String = URLConnection.guessContentTypeFromName(path)
        return mimeType.startsWith("video")
}

.zip or .rar:

private fun isZip(name: String): Boolean {
        return name.contains(".zip") || name.contains(".rar")
}

Upvotes: 0

Konstantin Konopko
Konstantin Konopko

Reputation: 5418

enum class MediaType {
    MediaTypeImage,
    MediaTypeVideo,
    Unknown
}

fun getMediaType(context: Context, source: Uri): MediaType {
    val mediaTypeRaw = context.contentResolver.getType(source)
    if (mediaTypeRaw?.startsWith("image") == true)
        return MediaType.MediaTypeImage
    if (mediaTypeRaw?.startsWith("video") == true)
        return MediaType.MediaTypeVideo
    return MediaType.Unknown
}

Upvotes: 3

tpaczesny
tpaczesny

Reputation: 676

It seems most proper to check type via ContentResolver (as in jsrssoftware answer). Although, this may return null in some cases.

In such case, I ended up trying to decode stream as Bitmap to confirm it is in image (but only decoding bounds, so it's quite fast and not much memory-consuming).

My image-tester helper function looks like this:

public static boolean checkIsImage(Context context, Uri uri) {
    ContentResolver contentResolver = context.getContentResolver();
    String type = contentResolver.getType(uri);
    if (type != null) {
        return  type.startsWith("image/");
    } else {
        // try to decode as image (bounds only)
        InputStream inputStream = null;
        try {
            inputStream = contentResolver.openInputStream(uri);
            if (inputStream != null) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(inputStream, null, options);
                return options.outWidth > 0 && options.outHeight > 0;
            }
        } catch (IOException e) {
            // ignore
        } finally {
            FileUtils.closeQuietly(inputStream);
        }
    }
    // default outcome if image not confirmed
    return false;
}

For videos, one could do similar approach. I did not need it, but I believe MediaMetadataRetriever could be used to verify if stream contains valid video in case type check fails.

Upvotes: 4

alfongj
alfongj

Reputation: 2273

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");
}

Upvotes: 88

metalmonkeysoftware
metalmonkeysoftware

Reputation: 636

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.

Upvotes: 18

Marko Niciforovic
Marko Niciforovic

Reputation: 3591

Easiest way is to check extensions I guess

if ( file.toString().endsWith(".jpg") {
//photo
} else if (file.toString().endsWith(".3gp")) {
//video
}

Upvotes: -52

Related Questions