/** get audios form {@link MediaStore.Audio.Media.EXTERNAL_CONTENT_URI} */ public List<MediaInfo> getAudioInfo() { List<MediaInfo> list = new ArrayList<MediaInfo>(); Cursor cursor = context .getContentResolver() .query( DreamConstant.AUDIO_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if (null == cursor) { } else { if (cursor.moveToFirst()) { MediaInfo mediaInfo = null; do { mediaInfo = new MediaInfo(); long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id String title = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); // 音乐标题 // Log.d("Yuri", title); String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家 String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); // 专辑 long albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长 long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小 String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径 int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐 if (isMusic != 0) { // 只把音乐添加到集合当中 mediaInfo.setId(id); mediaInfo.setDisplayName(title); mediaInfo.setArtist(artist); mediaInfo.setAlbum(album); mediaInfo.setAlbumId(albumId); mediaInfo.setDuration(duration); mediaInfo.setSize(size); mediaInfo.setUrl(url); list.add(mediaInfo); } } while (cursor.moveToNext()); } cursor.close(); } return list; }
/** get videos form {@link MediaStore.Video.Media.EXTERNAL_CONTENT_URI} */ public List<MediaInfo> getVideoInfo() { List<MediaInfo> list = new ArrayList<MediaInfo>(); ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = contentResolver.query( DreamConstant.VIDEO_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER); if (null == cursor) { } else { if (cursor.moveToFirst()) { MediaInfo mediaInfo = null; do { mediaInfo = new MediaInfo(); long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID)); long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION)); // 时长 long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE)); // 文件大小 String url = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA)); // 文件路径 String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME)); if (new File(url).exists()) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; // get video thumbail Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail( contentResolver, id, Images.Thumbnails.MICRO_KIND, options); mediaInfo.setId(id); mediaInfo.setDuration(duration); mediaInfo.setSize(size); mediaInfo.setUrl(url); mediaInfo.setDisplayName(displayName); mediaInfo.setIcon(bitmap); list.add(mediaInfo); } } while (cursor.moveToNext()); } cursor.close(); } return list; }