介紹

本篇介紹Android獲取本機各種類型文件的方法,已經封裝成工具類,末尾有源碼下載地址。

提示

獲取音樂、視頻、圖片、文檔等文件是需要有讀取SD卡的許可權的,如果是6.0以下的系統,則直接在清單文件中聲明SD卡讀取許可權即可;如果是6.0或以上,則需要動態申請許可權。

FileManager的使用

FileManager是封裝好的用於獲取本機各類文件的工具類,使用方式如:FileManager.getInstance(Context context).getMusics(),使用的是單例模式創建:

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;
}

獲取音樂列表

/**
* 獲取本機音樂列表
* @return
*/
public List<Music> getMusics() {
ArrayList<Music> musics = new ArrayList<>();
Cursor c = null;
try {
c = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

while (c.moveToNext()) {
String path = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));// 路徑

if (FileUtils.isExists(path)) {
continue;
}

String name = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); // 歌曲名
String album = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); // 專輯
String artist = c.getString(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); // 作者
long size = c.getLong(c.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));// 大小
int duration = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));// 時長
int time = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));// 歌曲的id
// int albumId = c.getInt(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

Music music = new Music(name, path, album, artist, size, duration);
musics.add(music);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
return musics;
}

FileUtils中判斷文件是否存在的方法isExists(String path),代碼為:

/**
* 判斷文件是否存在
* @param path 文件的路徑
* @return
*/
public static boolean isExists(String path) {
File file = new File(path);
return file.exists();
}

音樂的bean類Music代碼為:

public class Music implements Comparable<Music> {
/**歌曲名*/
private String name;
/**路徑*/
private String path;
/**所屬專輯*/
private String album;
/**藝術家(作者)*/
private String artist;
/**文件大小*/
private long size;
/**時長*/
private int duration;
/**歌曲名的拼音,用於字母排序*/
private String pinyin;

public Music(String name, String path, String album, String artist, long size, int duration) {
this.name = name;
this.path = path;
this.album = album;
this.artist = artist;
this.size = size;
this.duration = duration;
pinyin = PinyinUtils.getPinyin(name);
}

... //此處省略setter和getter方法
}

PinyinUtils根據名字獲取拼音,主要是用於音樂列表A-Z的排序,需要依賴pinyin4j.jar,獲取拼音的方法getPinyin(String name)代碼為:

public static String getPinyin(String str) {
// 設置拼音結果的格式
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);// 設置為大寫形式
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不用加入聲調

StringBuilder sb = new StringBuilder();

char[] charArray = str.toCharArray();

for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];

if (Character.isWhitespace(c)) {// 如果是空格則跳過
continue;
}

if (isHanZi(c)) {// 如果是漢字
String s = "";
try {
// toHanyuPinyinStringArray 返回一個字元串數組是因為該漢字可能是多音字,此處只取第一個結果
s = PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
sb.append(s);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
sb.append(s);
}

} else {
// 不是漢字
if (i == 0) {
if (isEnglish(c)) {// 第一個屬於字母,則返回該字母
return String.valueOf(c).toUpperCase(Locale.ENGLISH);
}
return "#"; // 不是的話返回#號
}
}
}
return sb.toString();
}

獲取視頻列表

/**
* 獲取本機視頻列表
* @return
*/
public List<Video> getVideos() {

List<Video> videos = new ArrayList<Video>();

Cursor c = null;
try {
// String[] mediaColumns = { "_id", "_data", "_display_name",
// "_size", "date_modified", "duration", "resolution" };
c = mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
while (c.moveToNext()) {
String path = c.getString(c.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));// 路徑
if (!FileUtils.isExists(path)) {
continue;
}

int id = c.getInt(c.getColumnIndexOrThrow(MediaStore.Video.Media._ID));// 視頻的id
String name = c.getString(c.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)); // 視頻名稱
String resolution = c.getString(c.getColumnIndexOrThrow(MediaStore.Video.Media.RESOLUTION)); //解析度
long size = c.getLong(c.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));// 大小
long duration = c.getLong(c.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));// 時長
long date = c.getLong(c.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_MODIFIED));//修改時間

Video video = new Video(id, path, name, resolution, size, date, duration);
videos.add(video);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
return videos;
}

其中,視頻的bean類Video代碼為:

public class Video {
private int id = 0;
private String path = null;
private String name = null;
private String resolution = null;// 解析度
private long size = 0;
private long date = 0;
private long duration = 0;

public Video(int id, String path, String name, String resolution, long size, long date, long duration) {
this.id = id;
this.path = path;
this.name = name;
this.resolution = resolution;
this.size = size;
this.date = date;
this.duration = duration;
}

... //此處省略setter和getter方法

通過本地視頻id獲取視頻縮略圖

// 獲取視頻縮略圖
public Bitmap getVideoThumbnail(int id) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MICRO_KIND, options);
return bitmap;
}

上面獲取視頻列表的方法中,Video對象中有一個屬性是id,通過傳入這個id可以獲取到視頻縮略圖的Bitmap對象。

獲取本機所有圖片文件夾

/**
* 得到圖片文件夾集合
*/
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;
}

其中,圖片文件夾的bean類ImgFolderBean代碼為:

public class ImgFolderBean {
/**當前文件夾的路徑*/
private String dir;
/**第一張圖片的路徑,用於做文件夾的封面圖片*/
private String fistImgPath;
/**文件夾名*/
private String name;
/**文件夾中圖片的數量*/
private int count;

public ImgFolderBean(String dir, String fistImgPath, String name, int count) {
this.dir = dir;
this.fistImgPath = fistImgPath;
this.name = name;
this.count = count;
}

... //此處省略setter和getter方法
}

獲取圖片文件夾下的圖片路徑的集合

/**
* 通過圖片文件夾的路徑獲取該目錄下的圖片
*/
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;
}

獲取本機已安裝應用列表

/**
* 獲取已安裝apk的列表
*/
public List<AppInfo> getAppInfos() {

ArrayList<AppInfo> appInfos = new ArrayList<AppInfo>();
//獲取到包的管理者
PackageManager packageManager = mContext.getPackageManager();
//獲得所有的安裝包
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);

//遍歷每個安裝包,獲取對應的信息
for (PackageInfo packageInfo : installedPackages) {

AppInfo appInfo = new AppInfo();

appInfo.setApplicationInfo(packageInfo.applicationInfo);
appInfo.setVersionCode(packageInfo.versionCode);

//得到icon
Drawable drawable = packageInfo.applicationInfo.loadIcon(packageManager);
appInfo.setIcon(drawable);

//得到程序的名字
String apkName = packageInfo.applicationInfo.loadLabel(packageManager).toString();
appInfo.setApkName(apkName);

//得到程序的包名
String packageName = packageInfo.packageName;
appInfo.setApkPackageName(packageName);

//得到程序的資源文件夾
String sourceDir = packageInfo.applicationInfo.sourceDir;
File file = new File(sourceDir);
//得到apk的大小
long size = file.length();
appInfo.setApkSize(size);

System.out.println("---------------------------");
System.out.println("程序的名字:" + apkName);
System.out.println("程序的包名:" + packageName);
System.out.println("程序的大小:" + size);

//獲取到安裝應用程序的標記
int flags = packageInfo.applicationInfo.flags;

if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//表示系統app
appInfo.setIsUserApp(false);
} else {
//表示用戶app
appInfo.setIsUserApp(true);
}

if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
//表示在sd卡
appInfo.setIsRom(false);
} else {
//表示內存
appInfo.setIsRom(true);
}

appInfos.add(appInfo);
}
return appInfos;
}

其中,安裝包信息的bean類AppInfo代碼為:

public class AppInfo {
private ApplicationInfo applicationInfo;
private int versionCode = 0;
/**
* 圖片的icon
*/
private Drawable icon;

/**
* 程序的名字
*/
private String apkName;

/**
* 程序大小
*/
private long apkSize;

/**
* 表示到底是用戶app還是系統app
* 如果表示為true 就是用戶app
* 如果是false表示系統app
*/
private boolean isUserApp;

/**
* 放置的位置
*/
private boolean isRom;

/**
* 包名
*/
private String apkPackageName;

... //此處省略setter和getter方法

獲取文檔、壓縮包、apk安裝包等

/**
* 通過文件類型得到相應文件的集合
**/
public List<FileBean> getFilesByType(int fileType) {
List<FileBean> files = new ArrayList<FileBean>();
// 掃描files文件庫
Cursor c = null;
try {
c = mContentResolver.query(MediaStore.Files.getContentUri("external"), new String[]{"_id", "_data", "_size"}, null, null, null);
int dataindex = c.getColumnIndex(MediaStore.Files.FileColumns.DATA);
int sizeindex = c.getColumnIndex(MediaStore.Files.FileColumns.SIZE);

while (c.moveToNext()) {
String path = c.getString(dataindex);

if (FileUtils.getFileType(path) == fileType) {
if (!FileUtils.isExists(path)) {
continue;
}
long size = c.getLong(sizeindex);
FileBean fileBean = new FileBean(path, FileUtils.getFileIconByPath(path));
files.add(fileBean);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
return files;
}

傳入的fileType文件類型是在FileUtils定義的文件類型聲明:

/**文檔類型*/
public static final int TYPE_DOC = 0;
/**apk類型*/
public static final int TYPE_APK = 1;
/**壓縮包類型*/
public static final int TYPE_ZIP = 2;

其中,FileUtils根據文件路徑獲取文件類型的方法getFileType(String path)為:

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;
}
}

文件的bean類FileBean代碼為:

public class FileBean {
/** 文件的路徑*/
public String path;
/**文件圖片資源的id,drawable或mipmap文件中已經存放doc、xml、xls等文件的圖片*/
public int iconId;

public FileBean(String path, int iconId) {
this.path = path;
this.iconId = iconId;
}
}

FileUtils根據文件類型獲取圖片資源id的方法,getFileIconByPath(path)代碼為:

/**通過文件名獲取文件圖標*/
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;
}

上述各種文件類型的圖片放置在mipmap中,用於展示文件列表時展示。

FileManager以及其他類的源碼,可以點擊下面的網址跳轉查看和下載:

點擊查看源碼(phone目錄下的文件)

作者:chaychan鏈接:imooc.com/article/23266來源:慕課網

本文原創發佈於慕課網 ,轉載請註明出處,謝謝合作


推薦閱讀:

接手別人的代碼,死的心有嗎?

普通的程序員和大神級的程序員有什麼區別?

網上黑程序員的現實依據是什麼?程序員真的那麼悲慘嗎?

有哪些視頻堪稱有毒?

暴露真實IP真的沒關係嗎?

有哪些程序員特有的習慣?

月薪3萬的程序員都避開了哪些坑?

和程序猿談戀愛是一種怎樣的體驗?


推薦閱讀:
相关文章