[android] 如何获取media文件的meta data信息
在app开发过程中,经常需要显示media文件的meta data信息,我们如何获取这些信息呢?
MediaStore
首先想到的就是MediaStore类了,它为我们提供的丰富的属性,我们只要通过sqlite操作,就能获取这些属性提供的值啦。
public static final String[] ItemImageProjection = new String[] { MediaStore.Images.ImageColumns.DISPLAY_NAME, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.SIZE, MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.MIME_TYPE }; public static final String[] ItemVideoProjection = new String[] { MediaStore.Video.VideoColumns.DISPLAY_NAME, MediaStore.Video.VideoColumns.DATA, MediaStore.Video.VideoColumns.SIZE, MediaStore.Video.VideoColumns._ID, MediaStore.Video.VideoColumns.MIME_TYPE }; public static final String[] ItemAudioProjection = new String[] { MediaStore.Audio.AudioColumns.DISPLAY_NAME, MediaStore.Audio.AudioColumns.DATA, MediaStore.Audio.AudioColumns.SIZE, MediaStore.Audio.AudioColumns._ID, MediaStore.Audio.AudioColumns.MIME_TYPE };
不知道如何查询系统数据库数据?查查ContentResolver怎么使用吧
不过有时候,这些属性还不能满足我们的需求,这时候就需要一些其他类来帮助了。那么,来看看其他类吧
MediaMetadataRetriever 类
SDK官方说明:MediaMetadataRetriever class provides a unified interface for retrieving frame and meta data from an input media file.
简单来说,我们可以重MediaMetadataRetriever类中获取meta data信息,关键是这些meta data信息是MediaStore类提供不了的,看看这个类能为我们提供哪些属性,写一个小demon测试下
public void getMetadata(String path){ MediaMetadataRetriever mmr = new MediaMetadataRetriever(); Log.d(TAG, "str:" + path); try { mmr.setDataSource(path); String album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); Log.d(TAG, "album:" + album); Log.d(TAG, "METADATA_KEY_ALBUMARTIST:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST)); String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); Log.d(TAG, "artist:" + artist); Log.d(TAG, "METADATA_KEY_AUTHOR:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR)); String bitrate = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); // 从api level 14才有,即从ICS4.0才有此功能 Log.d(TAG, "bitrate:" +bitrate); Log.d(TAG, "METADATA_KEY_CD_TRACK_NUMBER:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER)); Log.d(TAG, "METADATA_KEY_COMPILATION:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPILATION)); Log.d(TAG, "METADATA_KEY_COMPOSER:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER)); Log.d(TAG, "METADATA_KEY_DATE:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE)); Log.d(TAG, "METADATA_KEY_DISC_NUMBER:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER)); Log.d(TAG, "METADATA_KEY_DURATION:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); Log.d(TAG, "METADATA_KEY_GENRE:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)); Log.d(TAG, "METADATA_KEY_HAS_AUDIO:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO)); Log.d(TAG, "METADATA_KEY_HAS_VIDEO:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO)); Log.d(TAG, "METADATA_KEY_LOCATION:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION)); Log.d(TAG, "METADATA_KEY_MIMETYPE:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE)); Log.d(TAG, "METADATA_KEY_NUM_TRACKS:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS)); Log.d(TAG, "METADATA_KEY_TITLE:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)); Log.d(TAG, "METADATA_KEY_VIDEO_HEIGHT:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); Log.d(TAG, "METADATA_KEY_VIDEO_ROTATION:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)); Log.d(TAG, "METADATA_KEY_VIDEO_WIDTH:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); Log.d(TAG, "METADATA_KEY_WRITER:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_WRITER)); Log.d(TAG, "METADATA_KEY_YEAR:" + mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR)); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
然后,分别拿一个video和audio文件测试一下,结果如下:
video:找了一个mp4文件
04-16 11:00:04.997: D/MediaMetadataRetreiverDemon(15813): str:/storage/emulated/0/DCIM/100MEDIA/VIDEO0001.mp4 04-16 11:00:05.027: D/MediaMetadataRetreiverDemon(15813): album:null 04-16 11:00:05.027: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_ALBUMARTIST:null 04-16 11:00:05.027: D/MediaMetadataRetreiverDemon(15813): artist:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_AUTHOR:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): bitrate:21230041 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_CD_TRACK_NUMBER:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_COMPILATION:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_COMPOSER:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_DATE:20150416T021157.000Z 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_DISC_NUMBER:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_DURATION:11869 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_GENRE:null 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_HAS_AUDIO:yes 04-16 11:00:05.037: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_HAS_VIDEO:yes 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_LOCATION:null 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_MIMETYPE:video/mp4 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_NUM_TRACKS:2 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_TITLE:null 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_VIDEO_HEIGHT:1080 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_VIDEO_ROTATION:270 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_VIDEO_WIDTH:1920 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_WRITER:null 04-16 11:00:05.047: D/MediaMetadataRetreiverDemon(15813): METADATA_KEY_YEAR:null
audio:找了一个mp3文件
04-16 11:09:10.119: D/MediaMetadataRetreiverDemon(17550): str:/storage/emulated/0/Music/Nero MediaHome/Maid with the Flaxen Hair.mp3 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): album:Fine Music, Vol. 1 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_ALBUMARTIST:Richard Stoltzman 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): artist:Richard Stoltzman/Slovak Radio Symphony Orchestra 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_AUTHOR:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): bitrate:192000 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_CD_TRACK_NUMBER:2 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_COMPILATION:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_COMPOSER:Claude Debussy 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_DATE:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_DISC_NUMBER:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_DURATION:169697 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_GENRE:(32) 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_HAS_AUDIO:yes 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_HAS_VIDEO:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_LOCATION:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_MIMETYPE:audio/mpeg 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_NUM_TRACKS:1 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_TITLE:Maid with the Flaxen Hair 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_VIDEO_HEIGHT:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_VIDEO_ROTATION:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_VIDEO_WIDTH:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_WRITER:null 04-16 11:09:10.139: D/MediaMetadataRetreiverDemon(17550): METADATA_KEY_YEAR:2008
有这么多属性可以查到,应该够我们用了吧
知道了video和audio,还有image类型的文件呢,看看这个类ExifInterface
ExifInterface
sdk官方解释:This is a class for reading and writing Exif tags in a JPEG file.
如何使用这个类,很简单:
ExifInterface exifInterface; try { exifInterface = new ExifInterface(path); } catch (IOException e) { return; } exifInterface.getAttribute(ExifInterface.TAG_MODEL);
然后再来看看exifInterface为我们提供了哪些属性查询吧
http://developer.android.com/reference/android/media/ExifInterface.html
包括camera、gps信息,以及其他一些专业数据
通过这些工具,我们就基本能解决获取meta data信息的问题啦~
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。