model如下,功能一目了然:
public class FileInfo {// id value in DBpublic long id;// true if file is a directorypublic boolean isDir;// true if file is hiddenpublic boolean isHidden;// full name of the filepublic String fileName;// the path which the file inpublic String filePath;// file size in bytespublic long fileSize;// last modified timestamppublic long modifiedTimeMillis;// icon (adoptable)public Drawable icon;public boolean hasThumb;//if isDir is true, get file count in this dirpublic int fileCount;//resolution of picturespublic int height = 0;public int width = 0;public static FileInfo createInfo(File file, final boolean includeHidden) {FileInfo info = new FileInfo();if (file == null) {return info;}try {info.filePath = CanonicalPath();} catch (IOException e) {e.printStackTrace();info.filePath = AbsolutePath();}info.fileName = Name();info.fileSize = file.length();difiedTimeMillis = file.lastModified();info.isDir = file.isDirectory();info.isHidden = file.isHidden();info.fileCount = 0;if (info.isDir) {File[] list = file.listFiles(new FilenameFilter() {@Overridepublic boolean accept(File dir, String filename) {if (!includeHidden && filename.startsWith(".")) {return false;}return true;}});if (list != null) {info.fileCount = list.length;}} else {info.hasThumb = ThumbnailType(file) != null;}return info;}
}
获取内外置存储卡,OTG路径的方法(使用反射):
private List<FileInfo> goGetStorages() {List<FileInfo> list = new ArrayList<>();StorageManager sm = (StorageManager) SystemService(Context.STORAGE_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {Class<?> klass = StorageVolume.class;try {Method method = Method("getPathFile");List<StorageVolume> volumes = sm.getStorageVolumes();for (StorageVolume volume : volumes) {FileInfo info = ateInfo((File) method.invoke(volume), true);info.fileName = Description(mApplication);list.add(info);}} catch (Exception e) {e.printStackTrace();}} else {try {Class<?> klass = StorageManager.class;Method method = Method("getVolumePaths");String[] paths = (String[]) method.invoke(sm);if (paths != null) {for (String path : paths) {list.ateInfo(new File(path), true));}}} catch (Exception e) {e.printStackTrace();}}return list;}
本文发布于:2024-01-31 18:01:41,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170669530130352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |