-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tycw
committed
Jan 28, 2019
1 parent
768193e
commit 76d956c
Showing
12 changed files
with
638 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.taifua.material; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.Context; | ||
import android.database.Cursor; | ||
import android.provider.MediaStore; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileManager { | ||
private static FileManager mInstance; | ||
private static Context mContext; | ||
private static ContentResolver mContentResolver; | ||
private static Object mLock = new Object(); | ||
|
||
public static FileManager getInstance(Context context) { | ||
if (mInstance == null) { | ||
synchronized (mLock) { | ||
if (mInstance == null) { | ||
mInstance = new FileManager(); | ||
mContext = context; | ||
mContentResolver = context.getContentResolver(); | ||
} | ||
} | ||
} | ||
return mInstance; | ||
} | ||
|
||
|
||
/** | ||
* 通过图片文件夹的路径获取该目录下的图片 | ||
*/ | ||
public List<String> getImgListByDir(String dir) { | ||
ArrayList<String> imgPaths = new ArrayList<>(); | ||
File directory = new File(dir); | ||
if (directory == null || !directory.exists()) { | ||
return imgPaths; | ||
} | ||
File[] files = directory.listFiles(); | ||
for (File file : files) { | ||
String path = file.getAbsolutePath(); | ||
if (FileUtils.isPicFile(path)) { | ||
imgPaths.add(path); | ||
} | ||
} | ||
return imgPaths; | ||
} | ||
|
||
/** | ||
* 得到图片文件夹集合 | ||
*/ | ||
public List<ImgFolderBean> getImageFolders() { | ||
List<ImgFolderBean> folders = new ArrayList<ImgFolderBean>(); | ||
// 扫描图片 | ||
Cursor c = null; | ||
try { | ||
c = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, | ||
MediaStore.Images.Media.MIME_TYPE + "= ? or " + MediaStore.Images.Media.MIME_TYPE + "= ?", | ||
new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED); | ||
List<String> mDirs = new ArrayList<String>();//用于保存已经添加过的文件夹目录 | ||
while (c.moveToNext()) { | ||
String path = c.getString(c.getColumnIndex(MediaStore.Images.Media.DATA));// 路径 | ||
File parentFile = new File(path).getParentFile(); | ||
if (parentFile == null) | ||
continue; | ||
|
||
String dir = parentFile.getAbsolutePath(); | ||
if (mDirs.contains(dir))//如果已经添加过 | ||
continue; | ||
|
||
mDirs.add(dir);//添加到保存目录的集合中 | ||
ImgFolderBean folderBean = new ImgFolderBean(); | ||
folderBean.setDir(dir); | ||
folderBean.setFistImgPath(path); | ||
if (parentFile.list() == null) | ||
continue; | ||
int count = parentFile.list(new FilenameFilter() { | ||
@Override | ||
public boolean accept(File dir, String filename) { | ||
if (filename.endsWith(".jpeg") || filename.endsWith(".jpg") || filename.endsWith(".png")) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
}).length; | ||
|
||
folderBean.setCount(count); | ||
folders.add(folderBean); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (c != null) { | ||
c.close(); | ||
} | ||
} | ||
|
||
return folders; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.taifua.material; | ||
|
||
import android.os.Environment; | ||
|
||
import java.io.File; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
|
||
public class FileUtils { | ||
|
||
/**文档类型*/ | ||
public static final int TYPE_DOC = 0; | ||
/**apk类型*/ | ||
public static final int TYPE_APK = 1; | ||
/**压缩包类型*/ | ||
public static final int TYPE_ZIP = 2; | ||
|
||
|
||
/** | ||
* 判断文件是否存在 | ||
* @param path 文件的路径 | ||
* @return | ||
*/ | ||
public static boolean isExists(String path) { | ||
File file = new File(path); | ||
return file.exists(); | ||
} | ||
|
||
public static int getFileType(String path) { | ||
path = path.toLowerCase(); | ||
if (path.endsWith(".doc") || path.endsWith(".docx") || path.endsWith(".xls") || path.endsWith(".xlsx") | ||
|| path.endsWith(".ppt") || path.endsWith(".pptx")) { | ||
return TYPE_DOC; | ||
}else if (path.endsWith(".apk")) { | ||
return TYPE_APK; | ||
}else if (path.endsWith(".zip") || path.endsWith(".rar") || path.endsWith(".tar") || path.endsWith(".gz")) { | ||
return TYPE_ZIP; | ||
}else{ | ||
return -1; | ||
} | ||
} | ||
|
||
|
||
/**通过文件名获取文件图标 | ||
public static int getFileIconByPath(String path){ | ||
path = path.toLowerCase(); | ||
int iconId = R.mipmap.unknow_file_icon; | ||
if (path.endsWith(".txt")){ | ||
iconId = R.mipmap.type_txt; | ||
}else if(path.endsWith(".doc") || path.endsWith(".docx")){ | ||
iconId = R.mipmap.type_doc; | ||
}else if(path.endsWith(".xls") || path.endsWith(".xlsx")){ | ||
iconId = R.mipmap.type_xls; | ||
}else if(path.endsWith(".ppt") || path.endsWith(".pptx")){ | ||
iconId = R.mipmap.type_ppt; | ||
}else if(path.endsWith(".xml")){ | ||
iconId = R.mipmap.type_xml; | ||
}else if(path.endsWith(".htm") || path.endsWith(".html")){ | ||
iconId = R.mipmap.type_html; | ||
} | ||
return iconId; | ||
} | ||
*/ | ||
|
||
/**是否是图片文件*/ | ||
public static boolean isPicFile(String path){ | ||
path = path.toLowerCase(); | ||
if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/** 判断SD卡是否挂载 */ | ||
public static boolean isSDCardAvailable() { | ||
if (Environment.MEDIA_MOUNTED.equals(Environment | ||
.getExternalStorageState())) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* 从文件的全名得到文件的拓展名 | ||
* | ||
* @param filename | ||
* @return | ||
*/ | ||
public static String getExtFromFilename(String filename) { | ||
int dotPosition = filename.lastIndexOf('.'); | ||
if (dotPosition != -1) { | ||
return filename.substring(dotPosition + 1, filename.length()); | ||
} | ||
return ""; | ||
} | ||
/** | ||
* 读取文件的修改时间 | ||
* | ||
* @param f | ||
* @return | ||
*/ | ||
public static String getModifiedTime(File f) { | ||
Calendar cal = Calendar.getInstance(); | ||
long time = f.lastModified(); | ||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
cal.setTimeInMillis(time); | ||
// System.out.println("修改时间[2] " + formatter.format(cal.getTime())); | ||
// 输出:修改时间[2] 2009-08-17 10:32:38 | ||
return formatter.format(cal.getTime()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.taifua.material; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Images { | ||
private static List<ImgFolderBean> imgFolderList; | ||
public static List<String> imageThumbUrls = new ArrayList<>(); | ||
public static void init(Context context){ | ||
FileManager fileManager = FileManager.getInstance(context); | ||
imgFolderList = fileManager.getImageFolders(); | ||
for (ImgFolderBean imgFolderBean:imgFolderList){ | ||
for (String s:fileManager.getImgListByDir(imgFolderBean.getDir())) | ||
imageThumbUrls.add(s); | ||
} | ||
Log.d("图片总共有多少: ",""+imageThumbUrls.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.taifua.material; | ||
|
||
public class ImgFolderBean { | ||
/**当前文件夹的路径*/ | ||
private String dir; | ||
/**第一张图片的路径,用于做文件夹的封面图片*/ | ||
private String fistImgPath; | ||
/**文件夹名*/ | ||
private String name; | ||
/**文件夹中图片的数量*/ | ||
private int count; | ||
|
||
public ImgFolderBean(){} | ||
|
||
public ImgFolderBean(String dir, String fistImgPath, String name, int count) { | ||
this.dir = dir; | ||
this.fistImgPath = fistImgPath; | ||
this.name = name; | ||
this.count = count; | ||
} | ||
|
||
public String getDir() { | ||
return dir; | ||
} | ||
|
||
public void setDir(String dir) { | ||
this.dir = dir; | ||
} | ||
|
||
public String getFistImgPath() { | ||
return fistImgPath; | ||
} | ||
|
||
public void setFistImgPath(String fistImgPath) { | ||
this.fistImgPath = fistImgPath; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
public void setCount(int count) { | ||
this.count = count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.