Android获取本机各种类型文件列表(音乐、视频、图片、文档等)

本文介绍了如何在Android应用中获取视频缩略图,扫描并处理本地图片文件夹,获取已安装应用列表,以及根据文件类型获取相关文件。展示了获取多媒体数据和管理应用信息的关键代码片段。
摘要由CSDN通过智能技术生成

通过本地视频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 getImageFolders() {
    List folders = new ArrayList();
    // 扫描图片
    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 mDirs = new ArrayList();//用于保存已经添加过的文件夹目录
    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 getImgListByDir(String dir) {
    ArrayList 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 getAppInfos() {

ArrayList appInfos = new ArrayList();
//获取到包的管理者
PackageManager packageManager = mContext.getPackageManager();
//获得所有的安装包
List 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 getFilesByType(int fileType) {
    List files = new ArrayList();
    // 扫描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目录下的文件)

最后

如果你觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

希望读到这的您能转发分享和关注一下我,以后还会更新技术干货,谢谢您的支持!

转发+点赞+关注,第一时间获取最新知识点

Android架构师之路很漫长,一起共勉吧!

以下墙裂推荐阅读!!!

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)

总结

算法知识点繁多,企业考察的题目千变万化,面对越来越近的“金九银十”,我给大家准备好了一套比较完善的学习方法,希望能帮助大家在有限的时间里尽可能系统快速的恶补算法,通过高效的学习来提高大家面试中算法模块的通过率。

这一套学习资料既有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。

部分资料展示:




有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

有文字档也有视频,里面不仅仅有关键知识点的整理,还有案例的算法相关部分的讲解,可以帮助大家更好更全面的进行学习,二者搭配起来学习效果会更好。

部分资料展示:

[外链图片转存中…(img-lEyPYwqE-1711613480004)]
[外链图片转存中…(img-dUbVPQYL-1711613480004)]
[外链图片转存中…(img-Z1z4i3LU-1711613480004)]
[外链图片转存中…(img-rJNsahE6-1711613480004)]

有了这套学习资料,坚持刷题一周,你就会发现自己的算法知识体系有明显的完善,离大厂Offer的距离更加近。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值