2012-07-29 75 views
5

我知道单曲的URI。如何获取歌曲的信息,主题曲目等。Android如何从知道的URI获取单曲的ID信息

对不起,我能找到的所有文章都提供了索引整个库的方法,而不是如何获得一个知道URI歌曲的信息。这是我用来获取URI的。 audioID存储在SharedPrefs中。谢谢

int REQUEST_MEDIA = 0; 
String audioID; 

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 
if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) { 
     audioID = data.getDataString(); 

回答

5

我回答了我自己的问题,希望这可以帮助别人在未来。

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();  

    } 
} 
+0

它做到了!感谢您的跟进 - – kittka 2013-01-03 00:44:05

+0

谢谢,很高兴我能帮上忙。 – user1234051 2013-01-03 14:43:59

0

上面的答案只是不能让球继续我的不幸,但是我发现这对任何人仍然看。

//Some audio may be explicitly marked as not being music 
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; 

String[] projection = { 
    MediaStore.Audio.Media._ID, 
    MediaStore.Audio.Media.ARTIST, 
    MediaStore.Audio.Media.TITLE, 
    MediaStore.Audio.Media.DATA, 
    MediaStore.Audio.Media.DISPLAY_NAME, 
    MediaStore.Audio.Media.DURATION 
}; 

Cursor cursor = this.managedQuery(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
    projection, 
    selection, 
    null, 
    null); 

private List<String> songs = new ArrayList<String>(); 
while(cursor.moveToNext()) { 
    songs.add(cursor.getString(0) + "||" 
       + cursor.getString(1) + "||" 
       + cursor.getString(2) + "||" 
       + cursor.getString(3) + "||" 
       + cursor.getString(4) + "||" 
       + cursor.getString(5)); 

    //TO DISPLAY IN LISTVIEW 
    ListView lv = (ListView) findViewById(R.id.song_list); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songs); 
    lv.setAdapter(adapter); 

    cursor.close(); 

此代码是礼貌这个职位right here :-)

+0

此代码似乎查询所有音乐。该问题明确要求使用特定的URI。如果您认为此代码是合适的,请解释它,不要只是粘贴随机代码。 – Vince 2015-11-23 19:55:15

+0

@因为你是正确的,它确实会返回所有结果,并且不是随意的。我会更新以更好地适应今天的问题。 – 2015-11-24 00:37:18

相关问题