通过MediaStore查询image,video,arm,pdf等等文件数据

需要直接查询系统库来获取手机上的全部文件信息,如:图片,视频,音频,pdf文件等等。

直接上代码,获取文件的方法:

@SuppressLint("Range")
public ArrayList<DataBean> getFiles(Context context) {ArrayList<DataBean> files = new ArrayList<>();Cursor c = null;try {String select = null;Uri contentUri = null;if (mUriType.equalsIgnoreCase("image")) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if (mUriType.equalsIgnoreCase("video")) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if (mUriType.equalsIgnoreCase("audio")) {select = "(_data NOT LIKE '%.amr')";contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;} else if (mUriType.equalsIgnoreCase("file")) {// 当前需求是:只支持pdf文件select = "(_data LIKE '%.pdf')";contentUri = MediaStore.Files.getContentUri("external");}ContentResolver mContentResolver = context.getContentResolver();c = mContentResolver.query(contentUri, null, select, null, null);if (c == null || c.getCount() == 0) {return new ArrayList<>();}int columnIndexOrThrowId = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);int columnIndexOrThrowMimeType = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE);int columnIndexOrThrowData = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);int columnIndexOrThrowSize = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE);int columnIndexOrThrowTitle = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);@SuppressLint("InlinedApi")int columnIndexOrThrowDuration = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DURATION);int columnIndexOrThrowDataModified = c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED);while (c.moveToNext()) {String path = c.getString(columnIndexOrThrowData);String minType = c.getString(columnIndexOrThrowMimeType);int position_do = path.lastIndexOf(".");if (position_do == -1) {continue;}int position_x = path.lastIndexOf(File.separator);if (position_x == -1) {continue;}String displayName = path.substring(position_x + 1);long size = c.getLong(columnIndexOrThrowSize);if (size == 0 || (mUriType.equalsIgnoreCase("video") &&size > 100*1024*1024L)) {continue;}String title = c.getString(columnIndexOrThrowTitle);long duration = c.getLong(columnIndexOrThrowDuration);long modifieDate = c.getLong(columnIndexOrThrowDataModified);long id = c.getInt(columnIndexOrThrowId);Uri uri = ContentUris.withAppendedId(contentUri, id);File file = new File(path);String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()));DataBean info = new DataBean();info.setName(displayName);info.setTime(title);info.setMinType(minType);info.setUri(uri);info.setPath(path);info.setSize(size);info.setDuration(duration);info.setUriType(mUriType);info.setId(id);info.setTime(time);info.setModifiedDate(modifieDate);files.add(info);}} catch (Exception e) {e.printStackTrace();} finally {if (c != null) {c.close();}}return files;
}

DataBean.jav类


public class DataBean implements Parcelable {private long id;private String name;private String title;private Uri uri;private String path;private String thumbPath;private long size;private long duration;private String time;private String minType;private long modifiedDate;private String uriType;private boolean checked = false;public DataBean() {}public DataBean(String minType, Uri uri, long fileSize, long duration) {this.minType = minType;this.uri = uri;this.size = fileSize;this.duration = duration;}public DataBean(Parcel in) {id = in.readLong();name = in.readString();title = in.readString();uri = in.readParcelable(Uri.class.getClassLoader());path = in.readString();thumbPath = in.readString();size = in.readLong();duration = in.readLong();time = in.readString();minType = in.readString();modifiedDate = in.readLong();uriType = in.readString();checked = in.readByte() != 0;}public static final Creator<DataBean> CREATOR = new Creator<DataBean>() {@Overridepublic DataBean createFromParcel(Parcel in) {return new DataBean(in);}@Overridepublic DataBean[] newArray(int size) {return new DataBean[size];}};public boolean isChecked() {return checked;}public void setChecked(boolean checked) {this.checked = checked;}public String getUriType() {return uriType;}public void setUriType(String uriType) {this.uriType = uriType;}public String getThumbPath() {return thumbPath;}public void setThumbPath(String thumbPath) {this.thumbPath = thumbPath;}public long getDuration() {return duration / 1000;}public void setDuration(long duration) {this.duration = duration;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public long getModifiedDate() {return modifiedDate;}public void setModifiedDate(long modifiedDate) {this.modifiedDate = modifiedDate;}public String getMinType() {return minType;}public void setMinType(String minType) {this.minType = minType;}public Uri getUri() {return uri;}public void setUri(Uri uri) {this.uri = uri;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPath() {return path;}public void setPath(String path) {this.path = path;}public long getSize() {return size;}public void setSize(long size) {this.size = size;}public long getId() {return id;}public void setId(long id) {this.id = id;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}@Overridepublic String toString() {return "DataBean{" +"id=" + id +", name='" + name + '\'' +", title='" + title + '\'' +", uri=" + uri +", path='" + path + '\'' +", thumbPath='" + thumbPath + '\'' +", size=" + size +", duration=" + duration +", time='" + time + '\'' +", minType='" + minType + '\'' +", modifiedDate=" + modifiedDate +", uriType='" + uriType + '\'' +", checked=" + checked +'}';}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeLong(id);dest.writeString(name);dest.writeString(title);dest.writeParcelable(uri, flags);dest.writeString(path);dest.writeString(thumbPath);dest.writeLong(size);dest.writeLong(duration);dest.writeString(time);dest.writeString(minType);dest.writeLong(modifiedDate);dest.writeString(uriType);dest.writeByte((byte) (checked ? 1 : 0));}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/657698.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

collection、ofType、select的联合用法(Mybatis实现树状结构查询)

需求 得到树结构数据也可以用lambda表达式也行&#xff0c;也可以直接循环递归也行&#xff0c;本文采用的是直接在Mybatis层得到结果&#xff0c;各有各的优势。 代码 1、实体类 Data public class CourseChapterVO implements Serializable {private static final long s…

Java 字符串 07 练习-手机号屏蔽、身份证号信息查看,游戏骂人敏感词替换

注意点&#xff1a;只有返回值才是被截取的小串&#xff0c;所以需要有一个变量去承接它&#xff1b; 自己写的代码&#xff1a; import java.util.Scanner; public class practice {public static void main(String[] args) {Scanner input new Scanner(System.in);String …

Zookeeper分布式命名服务实战

目录 分布式命名服务 分布式API目录 分布式节点的命名 分布式的ID生成器 分布式的ID生成器方案&#xff1a; 基于Zookeeper实现分布式ID生成器 基于Zookeeper实现SnowFlakeID算法 分布式命名服务 命名服务是为系统中的资源提供标识能力。ZooKeeper的命名服务主要是利用Z…

ThreadLocal作用实例

ThreadLocal ThreadLocal表示线程的“局部变量”&#xff0c;它确保每个线程的ThreadLocal变量都是各自独立的&#xff0c;它提供了一种方法来创建只能被当前线程访问的变量。这意味着如果有两个不同的线程访问同一个ThreadLocal变量&#xff0c;那么这两个线程将不能看到彼此…

【issue-halcon例程学习】lines_gauss.hdev

例程功能 代码如下 dev_close_window () read_image (Angio, angio-part) get_image_size (Angio, Width, Height) dev_open_window (0, 0, 3 * Width / 2, 3 * Height / 2, black, WindowID) dev_display (Angio) dev_set_color (blue) MaxLineWidth : 8 Contrast : 12 calc…

Java中的线程安全

关于多线程并发环境下&#xff0c;数据的安全问题。 1.为什么这个是重点&#xff1f; 以后在开发中&#xff0c;我们的项目都是运行在服务器当中&#xff0c;而服务器已经将线程的定义&#xff0c;线程对象的创建&#xff0c;线程的启动等&#xff0c;都已经实现完了。这些代码…

QT自制软键盘 最完美、最简单、支持中文输入(二)

目录 一、前言 二、本自制虚拟键盘特点 三、中文输入原理 四、组合键输入 五、键盘事件模拟 六、界面 七、代码 7.1 frmKeyBoard 头文件代码 7.2 frmKeyBoard 源文件代码 八、使用示例 九、效果 十、结语 一、前言 由于系统自带虚拟键盘不一定好用&#xff0c;也不一…

【python】合理使用copy与deepcopy

合理使用**copy**与**deepcopy**对于dict和list等数据结构的对象&#xff0c;直接赋值使用的是引用的方式。而有些情况下需要复制整个对象&#xff0c;这时可以使用copy包里的copy和deepcopy&#xff0c;这两个函数的不同之处在于后者是递归复制的。效率也不一样&#xff1a;&a…

动态设置小程序IOS底部小黑条

创建setIOSAreaMixin.js文件 import {mapState,mapMutations } from vuexexport default {computed: {...mapState("ios_area", ["globalAreaClass", isSafeAreaCalculated])},mounted() {if (!this.isSafeAreaCalculated) {this.calculateSafeAreaClass(…

牛客网-----------[NOIP2006]数列

题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列&#xff0c;例如&#xff0c;当k3时&#xff0c;这个序列是&#xff1a; 1&#xff0c;3&#xff0c;4&#xff0c;9&#xff0c;10&#xff0c;12&#xff0c;13&…

2024年重庆市公务员考试报名明天开始,招聘4530人!

2024年重庆公务员招录公告已出&#xff0c;招聘人数&#xff1a;4530人 ✅重庆市考重要时间节点 报名时间&#xff1a;2月1日9:00-2月6日9:00 缴费时间&#xff1a;2月8日 笔试时间&#xff1a;3月16日-17日 笔试查成绩时间&#xff1a;4月15日 面试时间&#xff1a;4月27日-2…

C++文件操作(1)

C文件操作 1.文本的写入及读取文本文件写入文本文件读取 2.二进制文件的写入及读取二进制文件写入二进制文件读取 3.小结 C也有处理文件的能力&#xff0c;其功能实现依赖文件流。文件流是C中用来处理文件输入输出的一种流类。文件流可以用于从文件中读取数据或将数据写入到文件…

《游戏-03_2D-开发》

基于《游戏-02_2D-开发》&#xff0c; 继续制作游戏&#xff1a; 首先要做的时切割人物Idle空闲状态下的动画&#xff0c; 在切割之前我们需要创建一个文件夹&#xff0c;用来存放动画控制器AnimatorContoller&#xff0c; 再创建一个人物控制器文件夹用来存放人物控制器&…

【Ubuntu 22.04.3 LTS】apt-get下载安装有关问题可能原因及解决方法

ubuntu 22.04.3 LTS unaccountably error 装啥啥没依赖 可能是用了不合适的源&#xff0c;换个就好了 Now, let’s take a look at the lsb_release output, with a special focus on the Codename, which could be a crucial piece of information. The lsb_release comm…

认识BPMN2.0

&#x1f496;专栏简介 ✔️本专栏将从Camunda(卡蒙达) 7中的关键概念到实现中国式工作流相关功能。 ✔️文章中只包含演示核心代码及测试数据&#xff0c;完整代码可查看作者的开源项目snail-camunda ✔️请给snail-camunda 点颗星吧&#x1f618; &#x1f496;说在前面 …

STM32单片机基本原理与应用(四)

直流电机驱动控制原理 1、电机正反转控制 在STM32中&#xff0c;直流电机的正反转控制主要通过改变电机输入电源的极性来实现。当电机的电压极性发生变化时&#xff0c;电机的旋转方向也会相应改变。在硬件电路中&#xff0c;可以通过继电器或晶体管等电子开关来切换电机的电源…

查找二叉树(tree_a)

时间限制&#xff1a;1秒 内存限制&#xff1a;128M 题目描述 已知一棵二叉树用邻接表结构存储&#xff0c;中序查找二叉树中值为x的结点&#xff08;x在二叉树中是唯一的&#xff09;&#xff0c;并指出是第几个结点。 输入描述 第一行n为二叉树的结点个树&#xf…

【TCP】重传与超时机制

前言 在网络通信的世界里&#xff0c;传输控制协议&#xff08;TCP&#xff09;扮演着一个至关重要的角色。它确保了数据的可靠传输&#xff0c;就像邮差确保每一封信都能准确无误地送达收件人手中一样。但是&#xff0c;网络环境充满了不确定性&#xff0c;数据包可能会因为各…

新书速览|Docker与Kubernetes容器运维实战

帮助读者用最短的时间掌握Docker与K8s运维技能 内容简介 随着云计算和容器技术的发展&#xff0c;Docker与Kubernetes已经成为各个企业首选的部署工具&#xff0c;使用它们可以提高系统的部署效率和运维能力&#xff0c;降低运维成本。本书是一本为初学者量身定制的Docker与Kub…

Android PMS——PMS服务启动流程(二)

PackageManagerService 既然是系统服务&#xff0c;那么肯定是通过 SystemServer 启动的&#xff0c;所以我们首先看一下 SystemServer 服务中启动 PackageManagerService 相关代码。 一、PMS启动 1、SystemServer 源码路径&#xff1a;/frameworks/base/services/java/com/…