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

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

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

javascript 复制代码
    @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类

java 复制代码
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));
    }
}
相关推荐
阿巴斯甜12 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker13 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952714 小时前
Andorid Google 登录接入文档
android
黄林晴15 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_1 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android