之前文章中分析了Android media媒體庫,詳見:http://www.linuxidc.com/Linux/2015-03/114755.htm
這兒說一下怎麼樣在自己的應用程序調用系統提供的接口完成對指定媒體文件的掃描,約定:
指定的文件:就是指定路徑的文件(filepath);
掃描:獲取媒體的詳細信息,比如一首歌曲的歌手名、時長、專輯名等。
先看一下簡單的做法:
MediaScannerConnection.scanFile(mContext, new String[]{lastPath}, null,new MediaScanCompletedListener(){
@Override
public void onScanCompleted(String path, Uri uri) {
}
});
存在的問題:如果在一次調用sacnfile未結束的話,結束當前對象,沒辦法關閉回調接口,再者,使用不靈活,再次基礎上參考系統媒體庫,對這個調用進行重寫,如下:
一、指定掃描:
下面是我用到的方法,供參考:
import android.content.Context;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.net.Uri;
public class MediaScannerFile {
/**
* 掃描指定的文件
*
* @param context
* @param filePath
* @param sListener
*/
public static MediaScannerConnection scanFile(Context context, String[] filePath, String[] mineType,
OnScanCompletedListener sListener) {
ClientProxy client = new ClientProxy(filePath, mineType, sListener);
try {
MediaScannerConnection connection = new MediaScannerConnection(
context.getApplicationContext(), client);
client.mConnection = connection;
connection.connect();
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static class ClientProxy implements MediaScannerConnectionClient {
final String[] mPaths;
final String[] mMimeTypes;
final OnScanCompletedListener mClient;
MediaScannerConnection mConnection;
int mNextPath;
ClientProxy(String[] paths, String[] mimeTypes,
OnScanCompletedListener client) {
mPaths = paths;
mMimeTypes = mimeTypes;
mClient = client;
}
public void onMediaScannerConnected() {
scanNextPath();
}
public void onScanCompleted(String path, Uri uri) {
if (mClient != null) {
mClient.onScanCompleted(path, uri);
}
scanNextPath();
}
/**
* 自動掃描下一個
*/
void scanNextPath() {
if (mNextPath >= mPaths.length) {
mConnection.disconnect();
return;
}
String mimeType = mMimeTypes != null ? mMimeTypes[mNextPath] : null;
mConnection.scanFile(mPaths[mNextPath], mimeType);
mNextPath++;
}
}
}
調用:
if (null != mMediaScannerFile) {
mMediaScannerFile.scanFile(mContext, musicFilePaths, null,
this);
}
這裡也可以傳遞一個文件夾進去:
public void scanAllFile() {
String[] rootDir = new String[] { Environment.getExternalStorageDirectory()+"/test"};
mScanConnection = MediaScannerFile.scanFile(this, rootDir, null, this);
}
記得在退出時調用:
public void destroy() {
if (null != mScanConnection && mScanConnection.isConnected()) {
mScanConnection.disconnect();
}
}
二、獲取掃描完的媒體文件詳細信息:
先看媒體庫整體分析:http://www.linuxidc.com/Linux/2015-03/114755.htm
系統掃描完的數據全部放置在下面目錄所對應的數據庫中:
分internal和external兩個名詞,分別是機身內部存儲和外部存儲。
internal中存放了系統自帶的一些媒體,如:鈴聲、預置圖片、視頻等,一般是在
/system/media/目錄下。
external是除internal之外的存儲空間,內部sdcard、外部sdcard等;
我們導出external數據庫,查看一下有哪些表,都是做啥的,如下圖:
下面的例子是從audio表中查詢所有音樂的詳細信息:
public CenterMusicInfo queryMusicById(long id) {
Cursor cursor = mContext.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
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,MediaStore.Audio.Media.DATE_MODIFIED}
,MediaStore.Audio.Media._ID+"=? and ("+ MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE
+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=?)", new String[]{String.valueOf(id),"audio/mpeg","audio/mp4",
"audio/x-wav","audio/amr","audio/amr-wb","application/ogg","audio/aac","audio/x-ms-wma"},MediaStore.Audio.Media.DATE_MODIFIED+" desc");
CenterMusicInfo retMI = null;
try{
if (cursor.moveToFirst()) {
retMI = new CenterMusicInfo();
retMI.setId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
retMI.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));//文件名
retMI.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));//歌曲名
retMI.setDuration(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));//時長
retMI.setSinger(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));//歌手名
retMI.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));//專輯名
retMI.setYear(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)));//年代
retMI.setMimeType(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE)));
retMI.setFileSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)));
retMI.setFilePath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
retMI.setDateModified(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_MODIFIED)));
}
}finally{
if(cursor != null){
cursor.close();
}
}
return retMI;
}
public ArrayList<CenterMusicInfo> queryMusicList(int position,int limit) {
Uri.Builder builder = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.buildUpon();
builder.appendQueryParameter("limit", String.valueOf(limit));
Uri uri = builder.build();
Cursor cursor = mContext.getContentResolver().query(
uri,
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,MediaStore.Audio.Media.DATE_MODIFIED}
, MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE
+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=?", new String[]{"audio/mpeg","audio/mp4",
"audio/x-wav","audio/amr","audio/amr-wb","application/ogg","audio/aac","audio/x-ms-wma"},MediaStore.Audio.Media.DATE_MODIFIED+" desc");
ArrayList<CenterMusicInfo> musicList = new ArrayList<CenterMusicInfo>();
try{
if (cursor.moveToPosition(position)) {
do {
CenterMusicInfo musicInfo = new CenterMusicInfo();
musicInfo.setId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
musicInfo.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));//文件名
musicInfo.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));//歌曲名
musicInfo.setDuration(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));//時長
musicInfo.setSinger(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));//歌手名
musicInfo.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));//專輯名
musicInfo.setYear(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)));//年代
musicInfo.setMimeType(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE)));
musicInfo.setFileSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)));
musicInfo.setFilePath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
musicInfo.setDateModified(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_MODIFIED)));
musicList.add(musicInfo);
}while(cursor.moveToNext());
}
}finally{
if(cursor != null){
cursor.close();
}
}
return musicList;
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11