关闭

Android获取手机内的所有图片和视频

标签: Android获取图片获取视频工具类
136人阅读 评论(0) 收藏 举报
分类:

1.获取手机本地所有图片

  /**
     * 获取本地所有的图片
     *
     * @return list
     */
    public static List<Material> getAllLocalPhotos(Context context, int uid) {
        long totalUploadCount = MPSManager.getInstance(context).getMpsRecordCount(uid) + 1000;
        List<Material> list = new ArrayList<>();
        String[] projection = {
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.SIZE
        };
        //全部图片
        String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
                + MediaStore.Images.Media.MIME_TYPE + "=? or "
                + MediaStore.Images.Media.MIME_TYPE + "=?";
        //指定格式
        String[] whereArgs = {"image/jpeg", "image/png", "image/jpg"};
        //查询
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, where, whereArgs,
                MediaStore.Images.Media.DATE_MODIFIED + " desc ");
        if (cursor == null) {
            return list;
        }
        //遍历
        while (cursor.moveToNext()) {
            Material materialBean = new Material();
            //获取图片的名称
            materialBean.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
            long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)); // 大小

            //获取图片的生成日期
            byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));

            String path = new String(data, 0, data.length - 1);
            File file = new File(path);

            if (size < 5 * 1024 * 1024) {//<5M
                long time = file.lastModified();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String t = format.format(time);
                materialBean.setTime(t);
                materialBean.setLogo(path);
                materialBean.setFilePath(path);
                materialBean.setFileSize(size);
                materialBean.setChecked(false);
                materialBean.setFileType(6);
                materialBean.setFileId(totalUploadCount++);
                materialBean.setUploadedSize(0);
                materialBean.setTimeStamps(System.currentTimeMillis() + "");
                list.add(materialBean);
            }
        }
        cursor.close();
        return list;
    }


2.获取手机本地所有视频

  /**
     * 获取本地所有的视频
     *
     * @return list
     */
    public static List<Material> getAllLocalVideos(Context context, int uid) {
        long totalUploadCount = MPSManager.getInstance(context).getMpsRecordCount(uid) + 1000;
        String[] projection = {
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.SIZE
        };
        //全部图片
        String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=? or "
                + MediaStore.Video.Media.MIME_TYPE + "=?";
        String[] whereArgs = {"video/mp4", "video/3gp", "video/aiv", "video/rmvb", "video/vob", "video/flv",
                "video/mkv", "video/mov", "video/mpg"};
        List<Material> list = new ArrayList<>();
        Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                projection, where, whereArgs, MediaStore.Video.Media.DATE_ADDED + " DESC ");
        if (cursor == null) {
            return list;
        }
        try {
            while (cursor.moveToNext()) {
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); // 大小
                if (size < 600 * 1024 * 1024) {//<600M
                    Material materialBean = new Material();
                    String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); // 路径
                    long duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)); // 时长
                    materialBean.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)));
                    materialBean.setLogo(path);
                    materialBean.setFilePath(path);
                    materialBean.setChecked(false);
                    materialBean.setFileType(2);
                    materialBean.setFileId(totalUploadCount++);
                    materialBean.setUploadedSize(0);
                    materialBean.setTimeStamps(System.currentTimeMillis() + "");
                    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                    format.setTimeZone(TimeZone.getTimeZone("GMT+0"));
                    String t = format.format(duration);
                    materialBean.setTime(context.getString(R.string.video_len) + t);
                    materialBean.setFileSize(size);
                    list.add(materialBean);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
        return list;
    }


3.使用到的实体类

/**
 * Function:bean
 * Created by arthinking on 2017/6/26.
 */

public class Material implements Parcelable {
    private String mLogo;
    private String title;
    private String time;
    private String filePath;
    private boolean isChecked;
    private long fileSize;
    private long fileId;
    private long uploadedSize;
    private int fileType;
    private boolean uploaded;
    private int progress; //上传进度
    private String timeStamps; //时间戳
    private int flag;//上传标志 0-正常 1--网络错误 2--超时(除了0以为均为上传失败标识)


    public static Creator<Material> getCREATOR() {
        return CREATOR;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public boolean isUploaded() {
        return uploaded;
    }

    public void setUploaded(boolean uploaded) {
        this.uploaded = uploaded;
    }

    public int getFileType() {
        return fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }

    public long getUploadedSize() {
        return uploadedSize;
    }

    public void setUploadedSize(long uploadedSize) {
        this.uploadedSize = uploadedSize;
    }

    public long getFileSize() {
        return fileSize;
    }

    public void setFileSize(long fileSize) {
        this.fileSize = fileSize;
    }

    public long getFileId() {
        return fileId;
    }

    public void setFileId(long fileId) {
        this.fileId = fileId;
    }

    public String getLogo() {
        return mLogo;
    }

    public void setLogo(String logo) {
        mLogo = logo;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getTimeStamps() {
        return timeStamps;
    }

    public void setTimeStamps(String timeStamps) {
        this.timeStamps = timeStamps;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    @Override
    public String toString() {
        return "MaterialBean{" +
                "mLogo='" + mLogo + '\'' +
                ", title='" + title + '\'' +
                ", time='" + time + '\'' +
                ", filePath='" + filePath + '\'' +
                ", isChecked=" + isChecked +
                ", fileSize=" + fileSize +
                ", fileId=" + fileId +
                ", uploadedSize=" + uploadedSize +
                ", fileType=" + fileType +
                ", uploaded=" + uploaded +
                ", progress=" + progress +
                ", timeStamps='" + timeStamps + '\'' +
                ", flag='" + flag + '\'' +
                '}';
    }

    public Material() {
    }

    protected Material(Parcel in) {
        mLogo = in.readString();
        title = in.readString();
        time = in.readString();
        filePath = in.readString();
        isChecked = in.readByte() != 0;
        fileSize = in.readLong();
        fileId = in.readLong();
        uploadedSize = in.readLong();
        fileType = in.readInt();
        uploaded = in.readByte() != 0;
        progress = in.readInt();
        timeStamps = in.readString();
        flag = in.readInt();
    }

    public static final Creator<Material> CREATOR = new Creator<Material>() {
        @Override
        public Material createFromParcel(Parcel in) {
            return new Material(in);
        }

        @Override
        public Material[] newArray(int size) {
            return new Material[size];
        }
    };

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mLogo);
        dest.writeString(title);
        dest.writeString(time);
        dest.writeString(filePath);
        dest.writeByte((byte) (isChecked ? 1 : 0));
        dest.writeLong(fileSize);
        dest.writeLong(fileId);
        dest.writeLong(uploadedSize);
        dest.writeInt(fileType);
        dest.writeByte((byte) (uploaded ? 1 : 0));
        dest.writeInt(progress);
        dest.writeString(timeStamps);
        dest.writeInt(flag);
    }
}

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:506445次
    • 积分:8349
    • 等级:
    • 排名:第2498名
    • 原创:312篇
    • 转载:109篇
    • 译文:0篇
    • 评论:58条
    个人联系方式
    加入我的QQ群
    文章分类