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

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

  1. /**
  2. * 获取本地所有的图片
  3. *
  4. * @return list
  5. */
  6. public static List<Material> getAllLocalPhotos(Context context, int uid) {
  7. long totalUploadCount = MPSManager.getInstance(context).getMpsRecordCount(uid) + 1000;
  8. List<Material> list = new ArrayList<>();
  9. String[] projection = {
  10. MediaStore.Images.Media.DATA,
  11. MediaStore.Images.Media.DISPLAY_NAME,
  12. MediaStore.Images.Media.SIZE
  13. };
  14. //全部图片
  15. String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
  16. + MediaStore.Images.Media.MIME_TYPE + "=? or "
  17. + MediaStore.Images.Media.MIME_TYPE + "=?";
  18. //指定格式
  19. String[] whereArgs = { "image/jpeg", "image/png", "image/jpg"};
  20. //查询
  21. Cursor cursor = context.getContentResolver().query(
  22. MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, where, whereArgs,
  23. MediaStore.Images.Media.DATE_MODIFIED + " desc ");
  24. if (cursor == null) {
  25. return list;
  26. }
  27. //遍历
  28. while (cursor.moveToNext()) {
  29. Material materialBean = new Material();
  30. //获取图片的名称
  31. materialBean.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
  32. long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)); // 大小
  33. //获取图片的生成日期
  34. byte[] data = cursor.getBlob(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
  35. String path = new String(data, 0, data.length - 1);
  36. File file = new File(path);
  37. if (size < 5 * 1024 * 1024) { //<5M
  38. long time = file.lastModified();
  39. SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm");
  40. String t = format.format(time);
  41. materialBean.setTime(t);
  42. materialBean.setLogo(path);
  43. materialBean.setFilePath(path);
  44. materialBean.setFileSize(size);
  45. materialBean.setChecked( false);
  46. materialBean.setFileType( 6);
  47. materialBean.setFileId(totalUploadCount++);
  48. materialBean.setUploadedSize( 0);
  49. materialBean.setTimeStamps(System.currentTimeMillis() + "");
  50. list.add(materialBean);
  51. }
  52. }
  53. cursor.close();
  54. return list;
  55. }


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

  1. /**
  2. * 获取本地所有的视频
  3. *
  4. * @return list
  5. */
  6. public static List<Material> getAllLocalVideos(Context context, int uid) {
  7. long totalUploadCount = MPSManager.getInstance(context).getMpsRecordCount(uid) + 1000;
  8. String[] projection = {
  9. MediaStore.Video.Media.DATA,
  10. MediaStore.Video.Media.DISPLAY_NAME,
  11. MediaStore.Video.Media.DURATION,
  12. MediaStore.Video.Media.SIZE
  13. };
  14. //全部图片
  15. String where = MediaStore.Images.Media.MIME_TYPE + "=? or "
  16. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  17. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  18. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  19. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  20. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  21. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  22. + MediaStore.Video.Media.MIME_TYPE + "=? or "
  23. + MediaStore.Video.Media.MIME_TYPE + "=?";
  24. String[] whereArgs = { "video/mp4", "video/3gp", "video/aiv", "video/rmvb", "video/vob", "video/flv",
  25. "video/mkv", "video/mov", "video/mpg"};
  26. List<Material> list = new ArrayList<>();
  27. Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
  28. projection, where, whereArgs, MediaStore.Video.Media.DATE_ADDED + " DESC ");
  29. if (cursor == null) {
  30. return list;
  31. }
  32. try {
  33. while (cursor.moveToNext()) {
  34. long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); // 大小
  35. if (size < 600 * 1024 * 1024) { //<600M
  36. Material materialBean = new Material();
  37. String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); // 路径
  38. long duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)); // 时长
  39. materialBean.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)));
  40. materialBean.setLogo(path);
  41. materialBean.setFilePath(path);
  42. materialBean.setChecked( false);
  43. materialBean.setFileType( 2);
  44. materialBean.setFileId(totalUploadCount++);
  45. materialBean.setUploadedSize( 0);
  46. materialBean.setTimeStamps(System.currentTimeMillis() + "");
  47. SimpleDateFormat format = new SimpleDateFormat( "HH:mm:ss");
  48. format.setTimeZone(TimeZone.getTimeZone( "GMT+0"));
  49. String t = format.format(duration);
  50. materialBean.setTime(context.getString(R.string.video_len) + t);
  51. materialBean.setFileSize(size);
  52. list.add(materialBean);
  53. }
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. } finally {
  58. cursor.close();
  59. }
  60. return list;
  61. }


3.使用到的实体类

  1. /**
  2. * Function:bean
  3. * Created by arthinking on 2017/6/26.
  4. */
  5. public class Material implements Parcelable {
  6. private String mLogo;
  7. private String title;
  8. private String time;
  9. private String filePath;
  10. private boolean isChecked;
  11. private long fileSize;
  12. private long fileId;
  13. private long uploadedSize;
  14. private int fileType;
  15. private boolean uploaded;
  16. private int progress; //上传进度
  17. private String timeStamps; //时间戳
  18. private int flag; //上传标志 0-正常 1--网络错误 2--超时(除了0以为均为上传失败标识)
  19. public static Creator<Material> getCREATOR() {
  20. return CREATOR;
  21. }
  22. public int getProgress() {
  23. return progress;
  24. }
  25. public void setProgress(int progress) {
  26. this.progress = progress;
  27. }
  28. public boolean isUploaded() {
  29. return uploaded;
  30. }
  31. public void setUploaded(boolean uploaded) {
  32. this.uploaded = uploaded;
  33. }
  34. public int getFileType() {
  35. return fileType;
  36. }
  37. public void setFileType(int fileType) {
  38. this.fileType = fileType;
  39. }
  40. public long getUploadedSize() {
  41. return uploadedSize;
  42. }
  43. public void setUploadedSize(long uploadedSize) {
  44. this.uploadedSize = uploadedSize;
  45. }
  46. public long getFileSize() {
  47. return fileSize;
  48. }
  49. public void setFileSize(long fileSize) {
  50. this.fileSize = fileSize;
  51. }
  52. public long getFileId() {
  53. return fileId;
  54. }
  55. public void setFileId(long fileId) {
  56. this.fileId = fileId;
  57. }
  58. public String getLogo() {
  59. return mLogo;
  60. }
  61. public void setLogo(String logo) {
  62. mLogo = logo;
  63. }
  64. public String getTitle() {
  65. return title;
  66. }
  67. public void setTitle(String title) {
  68. this.title = title;
  69. }
  70. public String getTime() {
  71. return time;
  72. }
  73. public void setTime(String time) {
  74. this.time = time;
  75. }
  76. public boolean isChecked() {
  77. return isChecked;
  78. }
  79. public void setChecked(boolean checked) {
  80. isChecked = checked;
  81. }
  82. public String getFilePath() {
  83. return filePath;
  84. }
  85. public void setFilePath(String filePath) {
  86. this.filePath = filePath;
  87. }
  88. public String getTimeStamps() {
  89. return timeStamps;
  90. }
  91. public void setTimeStamps(String timeStamps) {
  92. this.timeStamps = timeStamps;
  93. }
  94. public int getFlag() {
  95. return flag;
  96. }
  97. public void setFlag(int flag) {
  98. this.flag = flag;
  99. }
  100. @Override
  101. public String toString() {
  102. return "MaterialBean{" +
  103. "mLogo='" + mLogo + '\'' +
  104. ", title='" + title + '\'' +
  105. ", time='" + time + '\'' +
  106. ", filePath='" + filePath + '\'' +
  107. ", isChecked=" + isChecked +
  108. ", fileSize=" + fileSize +
  109. ", fileId=" + fileId +
  110. ", uploadedSize=" + uploadedSize +
  111. ", fileType=" + fileType +
  112. ", uploaded=" + uploaded +
  113. ", progress=" + progress +
  114. ", timeStamps='" + timeStamps + '\'' +
  115. ", flag='" + flag + '\'' +
  116. '}';
  117. }
  118. public Material() {
  119. }
  120. protected Material(Parcel in) {
  121. mLogo = in.readString();
  122. title = in.readString();
  123. time = in.readString();
  124. filePath = in.readString();
  125. isChecked = in.readByte() != 0;
  126. fileSize = in.readLong();
  127. fileId = in.readLong();
  128. uploadedSize = in.readLong();
  129. fileType = in.readInt();
  130. uploaded = in.readByte() != 0;
  131. progress = in.readInt();
  132. timeStamps = in.readString();
  133. flag = in.readInt();
  134. }
  135. public static final Creator<Material> CREATOR = new Creator<Material>() {
  136. @Override
  137. public Material createFromParcel(Parcel in) {
  138. return new Material(in);
  139. }
  140. @Override
  141. public Material[] newArray( int size) {
  142. return new Material[size];
  143. }
  144. };
  145. @Override
  146. public int describeContents() {
  147. return 0;
  148. }
  149. @Override
  150. public void writeToParcel(Parcel dest, int flags) {
  151. dest.writeString(mLogo);
  152. dest.writeString(title);
  153. dest.writeString(time);
  154. dest.writeString(filePath);
  155. dest.writeByte(( byte) (isChecked ? 1 : 0));
  156. dest.writeLong(fileSize);
  157. dest.writeLong(fileId);
  158. dest.writeLong(uploadedSize);
  159. dest.writeInt(fileType);
  160. dest.writeByte(( byte) (uploaded ? 1 : 0));
  161. dest.writeInt(progress);
  162. dest.writeString(timeStamps);
  163. dest.writeInt(flag);
  164. }
  165. }

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/80920874