/**
* 外部存储中所有图片文件所占内存
*
* @return
*/
public long getPictureTotalSize() {
ArrayList<MemoryInfo> resultList = queryAllMediaList(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
long size = 0L;
for (MemoryInfo cInfo : resultList) {
File file = new File(cInfo.getFilePath());
if(null!=file &&file.exists()){
size += cInfo.getFileSize();
}
}
return size;
}
/**
* 外部存储中所有视频文件所占内存
*
* @return
*/
public long getVideoTotalSize() {
ArrayList<MemoryInfo> resultList = queryAllMediaList(MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
long size = 0L;
for (MemoryInfo cInfo : resultList) {
File file = new File(cInfo.getFilePath());
if(null!=file &&file.exists()){
size += cInfo.getFileSize();
}
}
return size;
}
public ArrayList<MemoryInfo> queryAllMediaList(Uri uri) {
//我们只需要两个字段:大小、文件路径
Cursor cursor = mContext.getContentResolver().query(
uri,new String[] { MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.DATA }, null, null, null);
ArrayList<MemoryInfo> musicList = new ArrayList<MemoryInfo>();
try{
if (cursor.moveToFirst()) {
do {
MemoryInfo mInfo = new MemoryInfo();
mInfo.setFileSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)));
mInfo.setFilePath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
}while(cursor.moveToNext());
}
}finally{
if(cursor != null){
cursor.close();
}
}
return musicList;
}
class MemoryInfo {
private long fileSize = 0L;
private String filePath = "";
public long getFileSize() {
return fileSize;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
}