android/安卓获取视频文件缩略图及时长


啥也不说了 , 直接 上代码 吧!


//获取指定文件夹下所有视频文件
    public static List<VideoInfo> getVideo(Context context, String fileAbsolutePath) {
        ArrayList<VideoInfo> list = new ArrayList<>();
        File file = new File(fileAbsolutePath);
        if (!file.exists()){
            Toast.makeText(context, "文件夹不存在", Toast.LENGTH_SHORT).show();
            return null;
        }
        File[] subFile = file.listFiles();

        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
            // 判断是否为文件夹
            if (!subFile[iFileLength].isDirectory()) {
                String filename = subFile[iFileLength].getName();
                System.out.println("----name = "+filename);
                // 判断是否为MP4结尾
                if (filename.trim().toLowerCase().endsWith(".mp4")||filename.trim().toLowerCase().endsWith(".3gp")||
                filename.trim().toLowerCase().endsWith(".avi")||filename.trim().toLowerCase().endsWith(".flv")) {
                    //list.add(subFile[iFileLength].g);
                    VideoInfo info = new VideoInfo();
                    info.setPath(subFile[iFileLength].getPath());
                    info.setTitle(subFile[iFileLength].getName());
                    info.setDuration(Long.valueOf(FileUtils.getVideoDuration(subFile[iFileLength].getPath())));
                    list.add(info);
                }
            }
        }
        return list;
    }
//根据路径得到视频缩略图
    public static Bitmap getVideoPhoto(String videoPath) {
        MediaMetadataRetriever media =new MediaMetadataRetriever();
        media.setDataSource(videoPath);
        Bitmap bitmap = media.getFrameAtTime();
        return bitmap;
    }

    //获取视频总时长
    public static String getVideoDuration(String path){
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        mmr.setDataSource(path);
        String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); //
        return duration;
    }


//下面是 VideoInfo的Bean类 

public class VideoInfo {
   /**
    *
    */
   private static final long serialVersionUID = -7920222595800367956L;
   private int id;
   private String title;
   private String album;
   private String artist;
   private String displayName;
   private String mimeType;
   private String path;
   private long size;
   private long duration;


   /**
    *
    */
   public VideoInfo() {
      super();
   }

  

   private boolean isSelect;
   public boolean isSelect() {
      return isSelect;
   }

   public void setSelect(boolean isSelect) {
      this.isSelect = isSelect;
   }
   public VideoInfo(int id, String title, String album, String artist,
                    String displayName, String mimeType, String path, long size, long duration) {
      super();
      this.id = id;
      this.title = title;
      this.album = album;
      this.artist = artist;
      this.displayName = displayName;
      this.mimeType = mimeType;
      this.path = path;
      this.size = size;
      this.duration = duration;
   }

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getAlbum() {
      return album;
   }

   public void setAlbum(String album) {
      this.album = album;
   }

   public String getArtist() {
      return artist;
   }

   public void setArtist(String artist) {
      this.artist = artist;
   }

   public String getDisplayName() {
      return displayName;
   }

   public void setDisplayName(String displayName) {
      this.displayName = displayName;
   }

   public String getMimeType() {
      return mimeType;
   }

   public void setMimeType(String mimeType) {
      this.mimeType = mimeType;
   }

   public String getPath() {
      return path;
   }

   public void setPath(String path) {
      this.path = path;
   }

   public long getSize() {
      return size;
   }

   public void setSize(long size) {
      this.size = size;
   }

   public long getDuration() {
      return duration;
   }

   public void setDuration(long duration) {
      this.duration = duration;
   }

}

发布了1 篇原创文章 · 获赞 1 · 访问量 2759
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览