使用Android 內建的方法來使用query
取得SD卡內完整的多媒體 / 影片 / 音樂 / 圖片 的詳細資訊

This article is mainly talking about how to use Cursor. query to fetch those media info in external.db
And an example : get album, album_artist in android code.
You can also use sqlite3 package, but be ware of the permission issue.
_______________________________________________

Android系統會預先掃描所有的多媒體檔案,並且把資訊建立成資料庫放在
/data/data/com.android.providers.media/database/...
底下, SD卡的內容會被放在 external.db
其他的系統音訊歸類在 internal.db
裡面有所有可以用的資訊
可以把它pull到本機上打開來看
也可以透過 resolver.query 來存取

例如取出專輯、該專輯的演出者的使用方法如下:


package wesely.albuminfo;

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;

	/** Called when the activity is first created. */
	@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.Albums.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.Albums.ALBUM);
			String src = c.getString(index);
			result.add(src);
			dispStr = dispStr +"專輯  "+(i+1)+" :  "+src + "\n";
			
			index = c.getColumnIndexOrThrow(MediaStore.Audio.Albums.ARTIST);
			src = c.getString(index);
			result.add(src);
			dispStr = dispStr +"演出者  :  "+src + "\n\n";

			c.moveToNext();
		}
		c.close();
		
		textView.setText(dispStr);
	}
}

Result:
____________________
0720-2

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