FileList

来源:互联网 发布:知乎 阑夕 老司机 编辑:程序博客网 时间:2024/05/22 03:14

package com.android.loop;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import android.util.Log;

public class FileList {
 public static final String VIDEO_FOLDER_PATH = PropagandaVedioSettings.path;
 private ArrayList<String> arrayList;
 private String[] videoFormatList = { "3g2", "3gp", "asf", "avi", "divx",
   "f4v", "flv", "m4v", "mkv", "mov", "mp4", "mpg", "rm", "rmvb",
   "rv", "ts", "webm", "wmv" };
 public String[] sourceList;
 private int sourceIndex = -1;// 当前视频文件下标

 public String getVideo(int sourceIndex) {
  getSourceIndex();
  String name = sourceList[sourceIndex];
  System.out.println("name="+name);
  return name;
 }

 public int getLength() {
  System.out.println("sourceList.length=" + sourceList.length);
  return sourceList.length;
 }

 public int getSourceIndex() {
  fillVideoList();
  System.out.println("sourceIndex--**"+sourceIndex);
  return sourceIndex;
 }

 public String[] getSourceList() {
  fillVideoList();
  return sourceList;
 }

 public void fillVideoList() {
  arrayList = new ArrayList<String>();
  File vedioFoldr = null;
  try {
   String path = "";
   String format = "";
   vedioFoldr = new File(VIDEO_FOLDER_PATH);
   if (vedioFoldr.exists()) {
    for (File file : vedioFoldr.listFiles()) {
     if (file.isFile()) {
      path = file.getAbsolutePath();
      format = path.substring(path.lastIndexOf('.') + 1);
      if (containFormat(format)) {
       arrayList.add(path);
      }
     }
    }
    sourceList = new String[arrayList.size()];
    arrayList.toArray(sourceList);
    if (sourceList.length > 0) {
     Arrays.sort(sourceList);
     sourceIndex = 0;
    }
    System.out.println("sourceIndex="+sourceIndex);
   }
   System.out.println("arrayList=" + arrayList);
  } catch (Exception e) {
   Log.e("fillVideoList", e.toString());
  }
 }

 private boolean containFormat(String format) {
  boolean b = false;
  for (String str : videoFormatList) {
   if (format.equals(str)) {
    b = true;
    break;
   }
  }
  return b;
 }
}

原创粉丝点击