Android获取managedQuery游标数据路径

时间:2013-08-04 18:48:12

标签: android android-activity cursor mediastore

我尝试在设备上显示我的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

请告诉我如何解决它

1 个答案:

答案 0 :(得分: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");