Android獲取本機各種類型文件的方法

介紹

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于網(wǎng)站設計、成都做網(wǎng)站、陵城網(wǎng)絡推廣、微信小程序定制開發(fā)、陵城網(wǎng)絡營銷、陵城企業(yè)策劃、陵城品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供陵城建站搭建服務,24小時服務熱線:13518219792,官方網(wǎng)址:muchs.cn

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

提示

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

FileManager的使用

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

private static FileManager mInstance;
private static Context mContext;
public 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
 */
private static 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根據(jù)名字獲取拼音,主要是用于音樂列表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 返回一個字符串數(shù)組是因為該漢字可能是多音字,此處只取第一個結果
    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
 */
private static 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獲取視頻縮略圖

// 獲取視頻縮略圖
private static 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對象。

獲取本機所有圖片文件夾

/**
 * 得到圖片文件夾集合
 */
private static 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>();//用于保存已經(jīng)添加過的文件夾目錄
  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))//如果已經(jīng)添加過
    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;
 /**文件夾中圖片的數(shù)量*/
 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方法
}

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

/**
 * 通過圖片文件夾的路徑獲取該目錄下的圖片
 */
private static 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的列表
 */
private static 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) {
   //表示系統(tǒng)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還是系統(tǒng)app
  * 如果表示為true 就是用戶app
  * 如果是false表示系統(tǒng)app
  */
 private boolean isUserApp;

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

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

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

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

/**
 * 通過文件類型得到相應文件的集合
 **/
private static 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;

其中,F(xiàn)ileUtils根據(jù)文件路徑獲取文件類型的方法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文件中已經(jīng)存放doc、xml、xls等文件的圖片*/
 public int iconId;

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

FileUtils根據(jù)文件類型獲取圖片資源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以及其他類的源碼,可以點擊下面的網(wǎng)址跳轉查看和下載:

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

分享標題:Android獲取本機各種類型文件的方法
文章地址:http://muchs.cn/article28/jiosjp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內鏈、做網(wǎng)站、響應式網(wǎng)站、網(wǎng)站策劃、域名注冊、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設