Question

I try to show list of my mp3 and wav files on my device. To play music on click, I try to save path of all my ringtones to mAudioPath[]. I get ListView resources string array with this:

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;
    }

But the line mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA) throws follewed exception:

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

Please tell me how to fix it

Was it helpful?

Solution

Change the projection to get MediaStore.Audio.Media.DATA column in the query.

  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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top