检查文件是否为图片或者视频

根据文件名后缀判断是否是视频文件:

java 复制代码
import java.util.Arrays;
import java.util.List;

/**
 * @class describe
 * @anthor alias.su
 * @time 2024/1/5 10:11
 * @change * @chang time * @class describe
 */
public class FileCheckUtil {
    // 定义常见的图片格式的护展名列表
    // 判断文件护展名是否为图片格式
    private static List<String> imageExtensions = Arrays.asList("jpg", "jpeg", "png", "gif");
    private static List<String> videoExtensions = Arrays.asList("mp4", "flv", "avi", "rm", "rmvb", "wmv");

    public static boolean isPic(String url) {
        boolean isImage = imageExtensions.contains(url.subSequence(url.lastIndexOf("."),url.length()).toString().replace(".","").toLowerCase());
        if (isImage) {
            return true;
        }
        return false;
    }

    public static boolean isVideo(String url) {
        boolean isImage = videoExtensions.contains(url.subSequence(url.lastIndexOf("."),url.length()).toString().replace(".","").toLowerCase());
        if (isImage) {
            return true;
        }
        return false;
    }

}

根据文件是否可以播放来判断:

java 复制代码
    public static boolean isVideo(File file) {
        int duration = 0;
        try {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            //                mmr.setDataSource(file.getAbsolutePath());
            mmr.setDataSource(Utils.getApp(), Uri.parse(file.getAbsolutePath()));
            duration = Integer.valueOf(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
            // 播放时长单位为毫秒
            LocalLogNew.d(TAG, "isVideo", 44, "检查是否为视频:" + duration);

        } catch (Exception e) {
            LocalLogNew.d(TAG, "isVideo", 44, "检查是否为视频报错:" + e.toString());
            duration = 0;
        }
        return duration > 0;
    }
相关推荐
alexhilton3 小时前
Jetpack Compose中的富文本输入
android·kotlin·android jetpack
兄弟加油,别颓废了。3 小时前
ctf.show_web4
android
哔哔龙5 小时前
Android OpenCV 实战:图片轮廓提取与重叠轮廓合并处理
android·算法
tangweiguo030519876 小时前
Android SSE 流式接收:从手写到框架的进阶之路
android
大尚来也6 小时前
PHP 反序列化漏洞深度解析:从原理利用到 allowed_classes 防御实战
android·开发语言·php
sp42a6 小时前
通过 RootEncoder 进行安卓直播 RTSP 推流
android·推流·rtsp
SY.ZHOU7 小时前
移动端架构体系(一):组件化
android·ios·架构·系统架构
恋猫de小郭8 小时前
Android 17 新适配要求,各大权限进一步收紧,适配难度提升
android·前端·flutter
流星白龙9 小时前
【MySQL】9.MySQL内置函数
android·数据库·mysql
进击的cc9 小时前
Android Kotlin:扩展函数如何优雅封装Android API
android·kotlin