0

我尝试在我的设备上显示我的mp3wav文件的列表。要在点击时播放音乐,我尝试将所有铃声的路径保存到mAudioPath[]. 我ListView用这个得到资源字符串数组:

private String[] getMusic() {
        mCursor = managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Media.DISPLAY_NAME}, 
                Audio.Media.DATA + " like ? OR " + Audio.Media.DATA + " like ? ",
                new String[] {"%mp3","%wav"}, 
                "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

        int count = mCursor.getCount();

        String[] songs = new String[count];
        int i = 0;
        if (mCursor.moveToFirst()) {
            do {
                songs[i] = mCursor.getString(0);
                mAudioPath[i] = mCursor.getString(mCursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                //--EXCEPTION--
                i++;
            } while (mCursor.moveToNext());
        }

        mCursor.close();

        return songs;
    }

但是该行mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)引发了以下异常:

W/dalvikvm(14188): threadid=1: thread exiting with uncaught exception (group=0x4001d560)
E/AndroidRuntime(14188): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.Ringtones}: java.lang.IllegalArgumentException: column '_data' does not exist
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.IllegalArgumentException: column '_data' does not exist
    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
    at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
    at com.example.myapp.Ringtones.getMusic(Ringtones.java:115)
    at com.example.myapp.Ringtones.onCreate(Ringtones.java:73)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more

请告诉我如何解决它

4

1 回答 1

1

更改投影以获取查询中的 MediaStore.Audio.Media.DATA 列。

  mCursor = managedQuery(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[] { MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA}, 
                    Audio.Media.DATA + " like ? OR " + Audio.Media.DATA + " like ? ",
                    new String[] {"%mp3","%wav"}, 
                    "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
于 2013-08-04T18:58:41.897 回答