[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資訊的問題啦~