This document is mainly talking about how to extract all music info in
/data/data/com.android.providers.media/databases
by using cursor.query.
(Android will prescan all media files' info in SD card, and write those info to "external.db")
---------------------
Android 系統會預先scan SD卡上全部的影音資料 (音樂/影片/圖片) 並且寫入到
/data/data/com.android.providers.media/databases/external.db
當中。
可以想辦法抓出來用資料庫軟體打開 (例如免費的 SQLite Expert)

裡面有許多的table
album_art              audio                  files
album_info             audio_genres           images
albums                 audio_genres_map       search
android_metadata       audio_genres_map_noid  searchhelpertitle
artist_info            audio_meta             thumbnails
artists                audio_playlists        video
artists_albums_map     audio_playlists_map    videothumbnails

這些table都可以在Android當中用resolver.query抓出來

在使用resolver.query抓Android媒體資料的時候的時候, 主要要下的指令:
Cursor c = resolver.query(uri, projection, selection, selectionArgs, sortOrder)

其中uri是必要的,依照使用需求來下
(類似指定要看哪個table)

* 抓音樂-音樂資料庫 (EXTERNAL代表SD卡)
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

* 抓音樂-專輯資料庫
MediaStore.Audio.Album.EXTERNAL_CONTENT_URI

* 抓所有多媒體檔案資料庫
MediaStore.Files.FileColumns.DATA

還有其他諸如此類的

selection 的話就是平常SQL語句 SELECT 後面那段字串

所以如果我今天要從全部音樂檔案裡面抓出Eminem的歌的話
我就這樣下:

<Cursor mediaCursor = 
	resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
		, null
		, "ARTIST=Eminem"		// SELECT "condition string"
		, null, null);

抓出來之後會得到一個對應的Cursor,指到符合條件的項目。
然後利用 cursor.moveToFirst(), moveToLast(), moveToNext(), moveTo(int i)
等等的指令來控制它的位置,然後再寫明要取出哪個column的資料:
int index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
再使用 getString(index)得到目標字串。
最後不要用的時候
c.close();


步驟如下:
// cursor 移動到第一個
c.moveToFirst();

// 取得column index
int index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);

// 取得內容字串
String src = c.getString(index);

// 結束的時候記得close
c.close();

完整取出音樂資訊的範例程式碼如下:

package wesely.testing;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.TextView;

public class TestingActivity extends Activity {
	private int index;
	private int totalCount;
	private ArrayList<String> result = new ArrayList<String>();
	private TextView textView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		textView = (TextView) findViewById(R.id.textView1);
		Context ctx = TestingActivity. this ;
		ContentResolver resolver = ctx.getContentResolver();
		Cursor c = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
		String dispStr = "";
		c.moveToFirst();
		totalCount = c.getCount();
		
		for ( int i = 0 ; i < totalCount; i ++ ){
			int index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
			String src = c.getString(index);
			result.add(src);
			dispStr = dispStr +"Title:   "+src + "\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
			src = c.getString(index);
			dispStr = dispStr +"Artist:  "+ src + "\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
			src = c.getString(index);
			dispStr = dispStr +"Album:  "+ src + "\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
			src = c.getString(index);
			int size = Integer.parseInt(src);
			size = size/1024;
			dispStr = dispStr +"Size:  "+ size + " kB\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
			src = c.getString(index);
			dispStr = dispStr +"Path:  "+ src + "\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
			int length = c.getInt(index);
			length = length/1000; // length in sec
			int sec = length%60;
			length = length-sec;
			int min = length/60;
			if (sec < 10)
				dispStr = dispStr +"Duration:  "+ min +":0"+ sec + "\n";
			else	
				dispStr = dispStr +"Duration:  "+ min +":"+ sec + "\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
			src = c.getString(index);
			dispStr = dispStr +"Data Type:  "+ src + "\n\n";
			c.moveToNext();
		}
		c.close();
		textView.setText(dispStr);
	}
}

執行結果

0720

arrow
arrow
    全站熱搜

    weselyong 發表在 痞客邦 留言(1) 人氣()