FileItem
public class FileItem implements Comparable<FileItem> {
private String mFilePath;
private String mFileName;
private String mSize;
/**
* 创建时间
*/
private String mDate;
private Type mType;
public FileItem(@NonNull String path, String name, String size, String date) {
this.mFilePath = path;
this.mFileName = name;
this.mSize = size;
this.mDate = date;
}
public String getFilePath() {
return mFilePath;
}
public void setFilePath(String mFilePath) {
this.mFilePath = mFilePath;
}
public String getFileName() {
return mFileName;
}
public void setFileName(String mFileName) {
this.mFileName = mFileName;
}
public String getFileSize() {
NumberFormat ddf1 = NumberFormat.getNumberInstance();
ddf1.setMaximumFractionDigits(2);
String sizeDisplay;
long size = Long.valueOf(mSize);
if (size > 1048576.0) {
double result = size / 1048576.0;
sizeDisplay = ddf1.format(result) + "M";
} else if (size > 1024) {
double result = size / 1024;
sizeDisplay = ddf1.format(result) + "K";
} else {
sizeDisplay = ddf1.format(size) + "B";
}
return sizeDisplay;
}
public long getLongFileSize() {
return Long.valueOf(mSize);
}
public String getDate() {
return mDate;
}
public Type getType() {
return mType;
}
public void setType(Type type) {
this.mType = type;
}
@Override
public int compareTo(@NonNull FileItem fileItem) {
return (int) (Long.valueOf(fileItem.getDate()) - Long.valueOf(mDate));
}
public enum Type {
Image(0), Video(1);
private final int code;
Type(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}
@Override
public String toString() {
if (mType == Type.Image) {
return "{mediaType: image, " + "mediaPath: " + mFilePath + "}";
} else {
return "{mediaType: video, " + "mediaPath: " + mFilePath + "}";
}
}
}
图片:
private boolean getPhotos() {
Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver contentResolver = getContext().getContentResolver();
String[] projection = new String[]{
MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.SIZE, MediaStore.Images.ImageColumns.DATE_ADDED
};
Cursor cursor = contentResolver.query(imageUri, projection, null, null,
MediaStore.Images.Media.DATE_ADDED + " desc");
if (cursor == null) {
return false;
}
if (cursor.getCount() != 0) {
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
String fileName =
cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
String size = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
String date = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
FileItem item = new FileItem(path, fileName, size, date);
item.setType(FileItem.Type.Image);
mMedias.add(item);
}
}
cursor.close();
return true;
}
视频
private boolean getVideos() {
Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = getContext().getContentResolver();
String[] projection = new String[]{
MediaStore.Video.VideoColumns.DATA, MediaStore.Video.VideoColumns.DURATION,
MediaStore.Video.VideoColumns.DISPLAY_NAME, MediaStore.Video.VideoColumns.DATE_ADDED
};
Cursor cursor = cr.query(videoUri, projection, null, null, null);
if (cursor == null) {
return false;
}
if (cursor.getCount() != 0) {
while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
String date = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATE_ADDED));
long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
VideoItem item = new VideoItem(path, name, null, date, duration);
item.setType(FileItem.Type.Video);
mMedias.add(item);
}
}
cursor.close();
return true;
}
网友评论