//筛选格式 这里可以放置你想扫描的文件类型.mp4 .mp3 .txt
private List<String> format=new ArrayList<>();
private List<HashMap<String,List<String>>> total=new ArrayList<>();
//每一个扫描的路径
private List<String> fileList = new ArrayList<>();
//文件夹名称
private List<String> str=new ArrayList<>();
//前面的Environment.getExternalStorageDirectory()是SD卡的路径 后面拼接的是指定文件夹
private String couplet=Environment.getExternalStorageDirectory()+"/Android/data/";
//这里的path参数是你要扫描的路径
File[] file = new File(path).listFiles();
//扫描文件 这里可以看到有两个参数 第一个是设置的当前路径扫描返回所有文件夹和文件
private void readFile(final File[] file,List<String> format) {
//这里面的操作是 将指定路径所有的文件和文件夹
for (int i = 0; i < file.length; i++) {
final File fi = file[i];
if (fi.isFile()) {
for (int j = 0; j <format.size() ; j++) {
if (fi.getName().endsWith(format.get(j))) {
fileList.add(fi.toString());
}
}
}
//如果是文件夹,递归扫描
else if (file[i].isDirectory()) {
final File[] newFileList = new File(file[i].getAbsolutePath()).listFiles();
readFile(newFileList,format);
// 通过多线程来加速
// new Thread(new Runnable() {
// @Override
// public void run() {
// readFile(newFileList);
// }
// }).start();
}
}
str.clear();
//得到文件对应的文件夹名称和创建对应的集合
for(int i=0 ; i<fileList.size(); i++) {
String s = fileList.get(i);
String[] split = s.split("/");
String s1 = split[split.length - 2];
if(str.size()==0){
str.add(s1);
HashMap<String,List<String>> name = new HashMap<>();
name.put(s1,new ArrayList<String>());
total.add(name);
}
else if(str.size()>0){
for (int j = 0; j <str.size() ; j++) {
if(str.get(j).equals(s1)){
istrue=false;
break;
}else {
istrue=true;
}
}
if(istrue==true){
str.add(s1);
HashMap<String,List<String>> name = new HashMap<>();
name.put(s1,new ArrayList<String>());
total.add(name);
}
}
}
}
public class toolkit {
//获得视频的缩略图
public Bitmap getVideoThumbnail(String videoPath) {
MediaMetadataRetriever media =new MediaMetadataRetriever();
media.setDataSource(videoPath);
Bitmap bitmap = media.getFrameAtTime();
return bitmap;
}
//读取视频时间
private String ReadVideoTime(File source) {
Encoder encoder = new Encoder();
String length = "";
try {
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration() / 1000;
int hour = (int) (ls / 3600);
int minute = (int) (ls % 3600) / 60;
int second = (int) (ls - hour * 3600 - minute * 60);
length = hour + "小时" + minute + "分钟" + second + "秒";
} catch (Exception e) {
e.printStackTrace();
Log.d("aaaa", "ReadVideoTime: "+e.toString());
}
return length;
}
//获得路径的Bitmap
private Bitmap getPathBitmap(String pathString)
{
Bitmap bitmap = null;
try
{
File file = new File(pathString);
if(file.exists())
{
bitmap = BitmapFactory.decodeFile(pathString);
}
} catch (Exception e)
{
}
return bitmap;
}
//读取文件大小
private String ReadVideoSize(File source) {
FileChannel fc= null;
String size = "";
try {
@SuppressWarnings("resource")
FileInputStream fis = new FileInputStream(source);
fc= fis.getChannel();
BigDecimal fileSize = new BigDecimal(fc.size());
size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null!=fc){
try{
fc.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
return size;
}
//详细信息
@SuppressLint("SimpleDateFormat")
private void initView(String path) {
// TODO Auto-generated method stub
File f = new File(path);
if (f.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
String time = new SimpleDateFormat("yyyy-MM-dd")
.format(new Date(f.lastModified()));
System.out.println("文件文件创建时间" + time);
System.out.println("文件大小:" + ShowLongFileSzie(f.length()));// 计算文件大小
// B,KB,MB,
System.out.println("文件大小:" + fis.available() + "B");
System.out.println("文件名称:" + f.getName());
System.out.println("文件是否存在:" + f.exists());
System.out.println("文件的相对路径:" + f.getPath());
System.out.println("文件的绝对路径:" + f.getAbsolutePath());
System.out.println("文件可以读取:" + f.canRead());
System.out.println("文件可以写入:" + f.canWrite());
System.out.println("文件上级路径:" + f.getParent());
System.out.println("文件大小:" + f.length() + "B");
System.out.println("文件最后修改时间:" + new Date(f.lastModified()));
System.out.println("是否是文件类型:" + f.isFile());
System.out.println("是否是文件夹类型:" + f.isDirectory());
} catch (Exception e) {
e.printStackTrace();
}
}
}
public String ShowLongFileSzie(Long length) {
if (length >= 1048576) {
return (length / 1048576)+"."+(length % 1048576)+ "MB";
} else if (length >= 1024) {
return (length / 1024) + "KB";
} else if (length < 1024) {
return length + "B";
} else {
return "0KB";
}
}
}
删除文件
private void doRemove() {
final File file = filedata.get(position);
judgeAlertDialog(context, "提醒", "你确认删除" + file.getName() + "吗(不可逆)?", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteDir(file);
filedata.remove(file);
notifyDataSetChanged();
showToast(file.getName() + " 删除成功");
}
}, null);
}
//扫描图片
public List<String> getPictures(final String strPath) {
List<String> list = new ArrayList<String>();
File file = new File(strPath);
File[] allfiles = file.listFiles();
if (allfiles == null) {
return null;
}
for(int i = 0; i < allfiles.length; i++) {
final File fi = allfiles[i];
if(fi.isFile()) {
// int idx = fi.getPath().lastIndexOf(".");
// if (idx <= 0) {
// continue;
// }
String suffix = fi.getPath().substring(fi.getPath().lastIndexOf("."));
if (suffix.toLowerCase().equals(".jpg") ||
suffix.toLowerCase().equals(".jpeg") ||
suffix.toLowerCase().equals(".bmp") ||
suffix.toLowerCase().equals(".png") ||
suffix.toLowerCase().equals(".gif") ) {
list.add(fi.getPath());
}
}
}
return list;
}
//扫描文件
private void readFile(final File[] file) {
for(int i=0 ; file!= null && i<file.length ;i++) {
//判读是否文件以及文件后缀名
if(file[i].isFile()){
if(file[i].getName().endsWith(".mp4")){
fileList.add(file[i].getName().toString());
}
}
//如果是文件夹,递归扫描
else if(file[i].isDirectory()) {
final File[] newFileList = new File(file[i].getAbsolutePath()).listFiles();
readFile(newFileList);
// 通过多线程来加速
// new Thread(new Runnable() {
// @Override
// public void run() {
// readFile(newFileList);
// }
// }).start();
}
}
}