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

需要直接查询系统库来获取手机上的全部文件信息,如:图片,视频,音频,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>pdf文件
                select = "(_data LIKE '%.pdf>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>() {
        @Override
        public DataBean createFromParcel(Parcel in) {
            return new DataBean(in);
        }

        @Override
        public 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;
    }

    @Override
    public 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 +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public 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.niftyadmin.cn/n/5354757.html

相关文章

flask+django基于python的网上美食订餐系统_3lyq1

设计旨在提高顾客就餐效率、优化餐厅管理、提高订单准确性和客户的满意度。本系统采用 Python 语言作为开发语言&#xff0c;采用Django框架及其第三方库和第三方工具来进行开发。该方案分为管理员功能模块&#xff0c;商家功能模块以及用户前后功能模块三部分。开发前期根据用…

STM32F407移植OpenHarmony笔记3

接上一篇&#xff0c;搭建完环境&#xff0c;找个DEMO能跑&#xff0c;现在我准备尝试从0开始搬砖。 首先把/device和/vendor之前的代码全删除&#xff0c;这个时候用hb set命令看不到任何项目了。 /device目录是硬件设备目录&#xff0c;包括soc芯片厂商和board板级支持代码…

华为手表应用APP开发:watch系列 GT系列 1.配置调试设备

表开发:GT3(1)配置调试设备 初环境与设备获取手表UUID登录 AppGallery Connect 点击用户与访问初 希望能写一些简单的教程和案例分享给需要的人 鸿蒙可穿戴开发 支持外包开发:xkk9866@yeah.net 环境与设备 系统:window 设备:HUAWEI WATCH 3 Pro 开发工具:DevEco St…

Java面试架构篇【一览众山小】

文章目录 &#x1f6a1; 简介☀️ Spring&#x1f425; 体系结构&#x1f420; 生命周期 &#x1f341; SpringMVC&#x1f330; 执行流程 &#x1f31c; SpringBoot&#x1f30d; 核心组件&#x1f38d; 自动装配&#x1f391; 3.0升级 &#x1f505; spring Cloud Alibaba&am…

学习使用Flask模拟接口进行测试

前言 学习使用一个新工具&#xff0c;首先找一段代码学习一下&#xff0c;基本掌握用法&#xff0c;然后再考虑每一部分是做什么的 Flask的初始化 app Flask(__name__)&#xff1a;初始化&#xff0c;创建一个该类的实例&#xff0c;第一个参数是应用模块或者包的名称 app…

【阿里巴巴】1688事业部-JAVA研发工程师-广告平台

所属部门:淘天集团 | 学历: 本科 | 工作年限: 2 年 职位描述 参与阿里B类电商广告平台的研发&#xff0c;能够独立承接项目并进行良好的系统设计和实现&#xff1b;通过对业务和技术栈的理解&#xff0c;对现有产品功能和系统架构进行改良和优化&#xff1b;从客户需求趋势和技…

qt 动态添加多个button按钮,并添加单击响应

qt动态添加多个button按钮简单&#xff0c;难题是如何对动态的按钮添加响应函数&#xff0c;本文解决方案有两个 方法一&#xff1a;使用信号-槽函数 QString strTemp;int nCol 6;//一行有6个for(int i 0; i < CZList.size(); i){int ii i / nCol;int jj i % nCol;strT…

burp靶场--xss下篇【16-30】

burp靶场–xss下篇【16-30】 https://portswigger.net/web-security/all-labs#cross-site-scripting 实验16&#xff1a;允许使用一些 SVG 标记的反射型 XSS ### 实验要求&#xff1a; 该实验室有一个简单的反射型 XSS漏洞。该网站阻止了常见标签&#xff0c;但错过了一些 S…