android list升降序排列 本地文件展示

private void ascendingSort(List<File> list) {
    Collections.sort(list, new Comparator<File>() {
        @Override
        public int compare(File p1, File p2) {
            // <是按照时间的降序排列 新拍的文件在最上面   相反将>
            if (p1.lastModified()< p2.lastModified()) {
                return 1;
            }
            if (p1.lastModified() == p2.lastModified()) {
                return 0;
            }
            return -1;
        }
    });
}

  // 获取当前目录下所有的mp4文件
    public List<String> getVideoFileName() {
        Vector<String> vecFile = new Vector<String>();
        File file = new File(FINALPATHVIDEO);
        File[] subFile = file.listFiles();
        //注意:本地文件数据源
        ascendingSort(Arrays.asList(subFile));
        if (subFile == null || subFile.length == 0) {
            mLinearViewEmpty.setVisibility(View.VISIBLE);
        } else {
            mLinearViewEmpty.setVisibility(View.GONE);
            if (mListPath == null) {
                mListPath = new ArrayList<>();
            }
            mListPath.clear();
            for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
                // 判断是否为文件夹
                if (!subFile[iFileLength].isDirectory()) {
                    String filename = subFile[iFileLength].getName();
                    String filenamepath = subFile[iFileLength].getPath();
//                    long filenamepathz = subFile[iFileLength].lastModified();
//                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String result = formatter.format(filenamepathz);
//                    Log.i("asdasd", "" + result + "---" + filenamepathz);
                    // 判断是否为MP4结尾
                    if (filename.trim().toLowerCase().endsWith(".mp4")) {
                        vecFile.add(filename);
                        mListPath.add(filenamepath);
                    }
                }
            }
        }

        return vecFile;
    }



猜你喜欢

转载自blog.csdn.net/liu3364575/article/details/79494034