2012-08-13 13 views
10

在我的應用程序中,我想製作一個選擇器,爲用戶提供選擇音樂的選擇。我想使用本地的android picker。我用下面的代碼來打開原生的Android音樂選擇器:如何打開音樂選取器?

final Intent intent2 = new Intent(Intent.ACTION_PICK); 
intent2.setType("audio/*"); 
startActivityForResult(intent2, 1); 

但是,當我執行它,我得到一個ActivityNotFoundException與此錯誤消息:

「你的手機有可用於沒有音樂庫選擇一個文件,請嘗試發送不同類型的文件」

難道我做錯了什麼呢?

回答

13

這個工作很適合我。

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(i,1); 

與Intent.ACTION_GET_CONTENT更普遍的意圖可以呈現給用戶多種選擇的活動,以選擇一個音頻文件(天文文件管理器等)。但是,用戶可以選擇任何文件,不一定音頻文件我想一個簡單的允許用戶選擇從媒體的音頻文件。這樣做的伎倆。

+0

優秀,請例如對於文件存儲 – 2013-06-27 18:44:32

+0

看到完整的例子在這裏:http://sudhanshuvinodgupta.blogspot.co.il/2012/07/using-intentactionpick.html – Guy 2014-04-08 08:41:02

+0

'Intent.ACTION_GET_CONTENT'的確是一個不錯的選擇器...它甚至可以讓我在OneDrive上打開一個文件。在比較中,Media.EXTERNAL_CONTENT_URI的'ACTION_PICK'更簡潔... **問題:**使用ACTION_PICK方法,如何預先選擇當前選擇的項目? (它顯示了沒有任何選擇的選取器) – 2015-12-17 14:21:36

0

東西沿着這一線路可能工作

// some Intent that points to whatever you like to play 
Intent play = new Intent(Intent.ACTION_VIEW); 
play.setData(Uri.fromFile(new File("/path/to/file"))); 
// create chooser for that intent 
try { 
    Intent i = Intent.createChooser(play, "Play Music"); 
    c.startActivity(i); 
} catch(ActivityNotFoundException ex) { 
    // if no app handles it, do nothing 
} 
4

如果你看看在AndroidManifest.xml文件的最新核心音樂應用程序,它可以揭示你有選擇的一些情況。例如:

<activity android:name="com.android.music.MusicPicker" 
     android:label="@string/music_picker_title" android:exported="true" > 
    <!-- First way to invoke us: someone asks to get content of 
     any of the audio types we support. --> 
    <intent-filter> 
     <action android:name="android.intent.action.GET_CONTENT" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.OPENABLE" /> 
     <data android:mimeType="audio/*"/> 
     <data android:mimeType="application/ogg"/> 
     <data android:mimeType="application/x-ogg"/> 
    </intent-filter> 
    <!-- Second way to invoke us: someone asks to pick an item from 
     some media Uri. --> 
    <intent-filter> 
     <action android:name="android.intent.action.PICK" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.OPENABLE" /> 
     <data android:mimeType="vnd.android.cursor.dir/audio"/> 
    </intent-filter> 
</activity> 

所以在此基礎上,你可以先嚐試

final Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT); 
intent2.setType("audio/*"); 
startActivityForResult(intent2, 1); 

,看看它是否符合您的需求。您還可以看看添加在上面的例子中提到的類別標誌,以幫助縮小結果(如OPENABLE應進行過濾,只可打開的流內容

相關問題