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

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

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


    @SuppressLint("Range")
public ArrayList getFiles(Context context) {
        ArrayList 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 CREATOR = new Creator() {
        @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));
    }
}

你可能感兴趣的:(pdf,android,查询文件,图片,视频,音频,查询image,pdf)