一种方式:解析媒体文件
android.media.MediaMetadataRetriever是Andorid内部的用于抽取视频/音频包含的标题、格式、艺术家等信息工具类.
//示例
MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY);
//时长
String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
//艺术家
String artist = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
//标题 String title = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
------------其它可以抽取的信息---------
public static final int METADATA_KEY_CD_TRACK_NUMBER = 0;
public static final int METADATA_KEY_ALBUM = 1;
public static final int METADATA_KEY_ARTIST = 2;
public static final int METADATA_KEY_AUTHOR = 3;
public static final int METADATA_KEY_COMPOSER = 4;
public static final int METADATA_KEY_DATE = 5;
public static final int METADATA_KEY_GENRE = 6;
public static final int METADATA_KEY_TITLE = 7;
public static final int METADATA_KEY_YEAR = 8;
public static final int METADATA_KEY_DURATION = 9;
public static final int METADATA_KEY_NUM_TRACKS = 10;
public static final int METADATA_KEY_IS_DRM_CRIPPLED = 11;
public static final int METADATA_KEY_CODEC = 12;
public static final int METADATA_KEY_RATING = 13;
public static final int METADATA_KEY_COMMENT = 14;
public static final int METADATA_KEY_COPYRIGHT = 15;
public static final int METADATA_KEY_BIT_RATE = 16;
public static final int METADATA_KEY_FRAME_RATE = 17;
public static final int METADATA_KEY_VIDEO_FORMAT = 18;
public static final int METADATA_KEY_VIDEO_HEIGHT = 19;
public static final int METADATA_KEY_VIDEO_WIDTH = 20;
public static final int METADATA_KEY_WRITER = 21;
二种方式:读取媒体文件数据库:
创建工具包:com.sky_dreaming.tools.media.provider
编写媒体信息封装类:MediaInfo.java
package com.sky_dreaming.tools.media.provider;
import java.io.UnsupportedEncodingException;
import android.graphics.Bitmap;
/**
* Media info beans
*
*
*/
public class MediaInfo {
/**
* play total time
*/
private int playDuration = 0;
/**
* song name
*/
private String mediaName = "";
/**
* album name
*/
private String mediaAlbum = "";
/**
* artist name
*/
private String mediaArtist = "";
/**
* mYear
*/
private String mediaYear = "";
/**
* fileName
*/
private String mFileName = "";
/**
* mFileType
*/
private String mFileType = "";
/**
* mFileSize
*/
private String mFileSize = "";
/**
* mFilePath
*/
private String mFilePath = "";
public Bitmap getmBitmap() {
return mBitmap;
}
public void setmBitmap(Bitmap mBitmap) {
this.mBitmap = mBitmap;
}
private Bitmap mBitmap = null;
/**
* getPlayDuration
*
* @return
*/
public int getPlayDuration() {
return playDuration;
}
/**
* setPlayDuration
*
* @param playDuration
*/
public void setPlayDuration(int playDuration) {
this.playDuration = playDuration;
}
/**
* getMediaName
*
* @param playDuration
*/
public String getMediaName() {
return mediaName;
}
/**
* setMediaName
*
* @param playDuration
*/
public void setMediaName(String mediaName) {
try {
mediaName =new String (mediaName.getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.mediaName = mediaName;
}
/**
* getMediaAlbum
*
* @param playDuration
*/
public String getMediaAlbum() {
return mediaAlbum;
}
/**
* setMediaAlbum
*
* @param playDuration
*/
public void setMediaAlbum(String mediaAlbum) {
try {
mediaAlbum =new String (mediaAlbum.getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.mediaAlbum = mediaAlbum;
}
/**
* getMediaArtist
*
* @param playDuration
*/
public String getMediaArtist() {
return mediaArtist;
}
/**
* setMediaArtist
*
* @param playDuration
*/
public void setMediaArtist(String mediaArtist) {
try {
mediaArtist =new String (mediaArtist.getBytes("ISO-8859-1"),"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.mediaArtist = mediaArtist;
}
/**
* getMediaYear
*
* @param playDuration
*/
public String getMediaYear() {
return mediaYear;
}
/**
* setMediaYear
*
* @param playDuration
*/
public void setMediaYear(String mediaYear) {
this.mediaYear = mediaYear;
}
/**
* getmFileName
*
* @param playDuration
*/
public String getmFileName() {
return mFileName;
}
/**
* setmFileName
*
* @param playDuration
*/
public void setmFileName(String mFileName) {
this.mFileName = mFileName;
}
/**
* getmFileType
*
* @param playDuration
*/
public String getmFileType() {
return mFileType;
}
/**
* setmFileType
*
* @param playDuration
*/
public void setmFileType(String mFileType) {
this.mFileType = mFileType;
}
/**
* getmFileSize
*
* @param playDuration
*/
public String getmFileSize() {
return mFileSize;
}
/**
* setmFileSize
*
* @param playDuration
*/
public void setmFileSize(String mFileSize) {
this.mFileSize = mFileSize;
}
/**
* getmFilePath
*
* @param playDuration
*/
public String getmFilePath() {
return mFilePath;
}
/**
* setmFilePath
*
* @param playDuration
*/
public void setmFilePath(String mFilePath) {
this.mFilePath = mFilePath;
}
}
编写数据提供工具类:MediaInfoProvider
package com.sky_dreaming.tools.media.provider;
import java.io.File;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.widget.Toast;
/**
* tools to get media file info
*
*
*/
public class MediaInfoProvider {
/**
* context
*/
private Context mContext = null;
/**
* data path
*/
private static final String dataPath = "/mnt";
/**
* query column
*/
private static final String[] mCursorCols = new String[] {
MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA };
/**
* MediaInfoProvider
* @param context
*/
public MediaInfoProvider(Context context) {
this.mContext = context;
}
/**
* get the media file info by path
* @param filePath
* @return
*/
public MediaInfo getMediaInfo(String filePath) {
/* check a exit file */
File file = new File(filePath);
if (file.exists()) {
Toast.makeText(mContext, "sorry, the file is not exit!",
Toast.LENGTH_SHORT);
}
/* create the query URI, where, selectionArgs */
Uri Media_URI = null;
String where = null;
String selectionArgs[] = null;
if (filePath.startsWith("content://media/")) {
/* content type path */
Media_URI = Uri.parse(filePath);
where = null;
selectionArgs = null;
} else {
/* external file path */
if(filePath.indexOf(dataPath) < 0) {
filePath = dataPath + filePath;
}
Media_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
where = MediaColumns.DATA + "=?";
selectionArgs = new String[] { filePath };
}
/* query */
Cursor cursor = mContext.getContentResolver().query(Media_URI,
mCursorCols, where, selectionArgs, null);
if (cursor == null || cursor.getCount() == 0) {
return null;
} else {
cursor.moveToFirst();
MediaInfo info = getInfoFromCursor(cursor);
printInfo(info);
return info;
}
}
/**
* get the media info beans from cursor
* @param cursor
* @return
*/
private MediaInfo getInfoFromCursor(Cursor cursor) {
MediaInfo info = new MediaInfo();
/* file name */
if(cursor.getString(1) != null) {
info.setmFileName(cursor.getString(1));
}
/* media name */
if(cursor.getString(2) != null) {
info.setMediaName(cursor.getString(2));
}
/* play duration */
if(cursor.getString(3) != null) {
info.setPlayDuration(cursor.getInt(3));
}
/* artist */
if(cursor.getString(4) != null) {
info.setMediaArtist(cursor.getString(4));
}
/* album */
if(cursor.getString(5) != null) {
info.setMediaAlbum(cursor.getString(5));
}
/* media year */
if (cursor.getString(6) != null) {
info.setMediaYear(cursor.getString(6));
} else {
info.setMediaYear("undefine");
}
/* media type */
if(cursor.getString(7) != null) {
info.setmFileType(cursor.getString(7).trim());
}
/* media size */
if (cursor.getString(8) != null) {
float temp = cursor.getInt(8) / 1024f / 1024f;
String sizeStr = (temp + "").substring(0, 4);
info.setmFileSize(sizeStr + "M");
} else {
info.setmFileSize("undefine");
}
/* media file path */
if (cursor.getString(9) != null) {
info.setmFilePath(cursor.getString(9));
}
return info;
}
/**
* print media info
* @param info
*/
private void printInfo(MediaInfo info) {
// TODO Auto-generated method stub
Log.i("playDuration","" + info.getPlayDuration());
Log.i("mediaName","" + info.getMediaName());
Log.i("mediaAlbum","" + info.getMediaAlbum());
Log.i("mediaArtist","" + info.getMediaArtist());
Log.i("mediaYear","" + info.getMediaYear());
Log.i("fileName","" + info.getmFileName());
Log.i("fileType","" + info.getmFileType());
Log.i("fileSize","" + info.getmFileSize());
Log.i("filePath","" + info.getmFilePath());
}
}
可以避免乱码