我回答了自己的问题,希望这对将来的其他人有所帮助。
int REQUEST_MEDIA = 0;
SharedPreferences prefs;
String prefName = "MyPref";
String audioID, artist_name, artist_band;
Uri MyUri;
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
audioID = data.getDataString();
MyUri = Uri.parse(audioID);
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Artists.ARTIST };
Cursor tempCursor = managedQuery(MrUri,
proj, null, null, null);
tempCursor.moveToFirst(); //reset the cursor
int col_index=-1;
int numSongs=tempCursor.getCount();
int currentNum=0;
do{
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
artist_name = tempCursor.getString(col_index);
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
artist_band = tempCursor.getString(col_index);
//do something with artist name here
//we can also move into different columns to fetch the other values
currentNum++;
}while(tempCursor.moveToNext());
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("alarm_selected", audioID);
editor.putString("alarm_name", artist_name);
editor.putString("alarm_band", artist_band);
editor.commit();
}
}