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

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

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;
    }
相关推荐
下位子4 分钟前
『OpenGL学习滤镜相机』- Day2: 渲染第一个三角形
android·opengl
风语者日志1 小时前
[LitCTF 2023]这是什么?SQL !注一下 !
android·数据库·sql
2501_915921432 小时前
iOS 26 CPU 使用率监控策略 多工具协同构建性能探索体系
android·ios·小程序·https·uni-app·iphone·webview
狂团商城小师妹2 小时前
JAVA国际版同城打车源码同城服务线下结账系统源码适配PAD支持Android+IOS+H5
android·java·ios·小程序·交友
游戏开发爱好者82 小时前
iOS 应用逆向对抗手段,多工具组合实战(iOS 逆向防护/IPA 混淆/无源码加固/Ipa Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
虚伪的空想家2 小时前
ip网段扫描机器shell脚本
android·linux·网络协议·tcp/ip·shell·脚本·network
generallizhong2 小时前
android TAB切换
android·gitee
00后程序员张3 小时前
iOS 文件管理与导出实战,多工具协同打造高效数据访问与调试体系
android·macos·ios·小程序·uni-app·cocoa·iphone
Boop_wu3 小时前
[MySQL] JDBC
android
qq_717410014 小时前
FAQ09075:6572平台相机拍照,拍下来的照片无法查看,图库查看时提示“无缩略图”
android