Android media媒体库分析之:分类别统计媒体文件大(3)

/**
  * 外部存储中所有图片文件所占内存
  *
  * @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;
  }
 }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/0f062c5a9a7e8d96a8d365d19af0263c.html