我找到了这个例子: http: //xjaphx.wordpress.com/2011/06/28/query-mp3-information-using-android-supported-function/ for MediaMetadataRetriever,它工作正常。但是当我尝试使用它形成一个循环并将数据添加到 ArrayList 时,我收到了这个错误:
04-25 20:20:27.121: E/AndroidRuntime(20692): FATAL EXCEPTION: main
04-25 20:20:27.121: E/AndroidRuntime(20692): java.lang.NullPointerException
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.ListView.makeAndAddView(ListView.java:1831)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.ListView.fillDown(ListView.java:674)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.ListView.fillFromTop(ListView.java:735)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.ListView.layoutChildren(ListView.java:1652)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.View.layout(View.java:14003)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewGroup.layout(ViewGroup.java:4375)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.Choreographer.doFrame(Choreographer.java:532)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.os.Handler.handleCallback(Handler.java:725)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.os.Looper.loop(Looper.java:137)
04-25 20:20:27.121: E/AndroidRuntime(20692): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-25 20:20:27.121: E/AndroidRuntime(20692): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 20:20:27.121: E/AndroidRuntime(20692): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 20:20:27.121: E/AndroidRuntime(20692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-25 20:20:27.121: E/AndroidRuntime(20692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-25 20:20:27.121: E/AndroidRuntime(20692): at dalvik.system.NativeStart.main(Native Method)
这是带有 MediaMetadataRetriever 的类:
public class GenreListing extends Activity {
ListView listView;
ArrayAdapter<String> adapter;
private SongsManager songManager;
private Utilities utils;
private List<String> songs = new ArrayList<String>();
private static final String SD_PATH = new String ("/sdcard/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.genre_list);
ListView lv = (ListView) findViewById(R.id.genreOutput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, songs);
lv.setAdapter(adapter);
songManager = new SongsManager();
utils = new Utilities();
updatePlaylist();
}
private void updatePlaylist() {
File home = new File(SD_PATH);
String out = "";
if (home.listFiles(new Mp3Filter()).length > 0) {
for (File file : home.listFiles(new Mp3Filter())) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(file.getPath());
String genre = "";
//String genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
songs.add(genre);
mmr.release();
}
}
}
}