Android获取手机内的所有图片和视频
其他
2018-07-08 20:11:11
阅读次数: 0
1.获取手机本地所有图片
-
-
-
-
-
-
public static List<Material> getAllLocalPhotos(Context context, int uid) {
-
long totalUploadCount = MPSManager.getInstance(context).getMpsRecordCount(uid) +
1000;
-
List<Material> list =
new ArrayList<>();
-
-
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 ");
-
-
-
-
-
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) {
-
long time = file.lastModified();
-
SimpleDateFormat format =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
-
String t = format.format(time);
-
-
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() +
"");
-
-
-
-
-
-
2.获取手机本地所有视频
-
-
-
-
-
-
public static List<Material> getAllLocalVideos(Context context, int uid) {
-
long totalUploadCount = MPSManager.getInstance(context).getMpsRecordCount(uid) +
1000;
-
-
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 ");
-
-
-
-
-
while (cursor.moveToNext()) {
-
long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
-
if (size <
600 *
1024 *
1024) {
-
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);
-
-
-
-
-
-
-
-
-
-
3.使用到的实体类
-
-
-
-
-
-
public
class Material implements Parcelable {
-
-
-
-
-
private
boolean isChecked;
-
-
-
private
long uploadedSize;
-
-
private
boolean uploaded;
-
-
private String timeStamps;
-
-
-
-
public static Creator<Material> getCREATOR() {
-
-
-
-
public int getProgress() {
-
-
-
-
public void setProgress(int progress) {
-
this.progress = progress;
-
-
-
public boolean isUploaded() {
-
-
-
-
public void setUploaded(boolean uploaded) {
-
this.uploaded = uploaded;
-
-
-
public int getFileType() {
-
-
-
-
public void setFileType(int fileType) {
-
this.fileType = fileType;
-
-
-
public long getUploadedSize() {
-
-
-
-
public void setUploadedSize(long uploadedSize) {
-
this.uploadedSize = uploadedSize;
-
-
-
public long getFileSize() {
-
-
-
-
public void setFileSize(long fileSize) {
-
this.fileSize = fileSize;
-
-
-
public long getFileId() {
-
-
-
-
public void setFileId(long fileId) {
-
-
-
-
public String getLogo() {
-
-
-
-
public void setLogo(String logo) {
-
-
-
-
public String getTitle() {
-
-
-
-
public void setTitle(String title) {
-
-
-
-
public String getTime() {
-
-
-
-
public void setTime(String time) {
-
-
-
-
public boolean isChecked() {
-
-
-
-
public void setChecked(boolean checked) {
-
-
-
-
public String getFilePath() {
-
-
-
-
public void setFilePath(String filePath) {
-
this.filePath = filePath;
-
-
-
public String getTimeStamps() {
-
-
-
-
public void setTimeStamps(String timeStamps) {
-
this.timeStamps = timeStamps;
-
-
-
-
-
-
-
public void setFlag(int flag) {
-
-
-
-
-
public String toString() {
-
-
"mLogo='" + mLogo +
'\'' +
-
", title='" + title +
'\'' +
-
", time='" + time +
'\'' +
-
", filePath='" + filePath +
'\'' +
-
", isChecked=" + isChecked +
-
", fileSize=" + fileSize +
-
-
", uploadedSize=" + uploadedSize +
-
", fileType=" + fileType +
-
", uploaded=" + uploaded +
-
", progress=" + progress +
-
", timeStamps='" + timeStamps +
'\'' +
-
", flag='" + flag +
'\'' +
-
-
-
-
-
-
-
protected Material(Parcel in) {
-
-
-
-
filePath = in.readString();
-
isChecked = in.readByte() !=
0;
-
fileSize = in.readLong();
-
-
uploadedSize = in.readLong();
-
-
uploaded = in.readByte() !=
0;
-
-
timeStamps = in.readString();
-
-
-
-
public
static
final Creator<Material> CREATOR =
new Creator<Material>() {
-
-
public Material createFromParcel(Parcel in) {
-
-
-
-
-
public Material[] newArray(
int size) {
-
return
new Material[size];
-
-
-
-
-
public int describeContents() {
-
-
-
-
-
public void writeToParcel(Parcel dest, int flags) {
-
-
-
-
dest.writeString(filePath);
-
dest.writeByte((
byte) (isChecked ?
1 :
0));
-
dest.writeLong(fileSize);
-
-
dest.writeLong(uploadedSize);
-
-
dest.writeByte((
byte) (uploaded ?
1 :
0));
-
-
dest.writeString(timeStamps);
-
-
-
-
转载自blog.csdn.net/mp624183768/article/details/80920874