0

我正在尝试在字符串数组中获取 sd 卡中存在的所有歌曲的路径,但它没有存储在数组中,否则它会覆盖它,请帮助我,代码....

String[] projection = new String[] {MediaStore.Audio.Media.DATA};
    Cursor mCur = managedQuery(Media.EXTERNAL_CONTENT_URI,projection, null, null, null);
    mCur.moveToFirst();
    path=new String[mCur.getColumnCount()]; 
    while (mCur.isAfterLast() == false) {       
    for (i=0; i<mCur.getColumnCount(); i++) 
    {path[i]=mCur.getString(i);
            System.out.println(",,,,,,,,"+mCur.getColumnCount());
            System.out.println(path[i]+i);}
            mCur.moveToNext(); }
4

2 回答 2

1

试试这个,应该做你想做的

String[] projection = new String[] {MediaStore.Audio.Media.DATA};
Cursor mCur = managedQuery(Media.EXTERNAL_CONTENT_URI,projection, null, null, null);
// check if there is data returned or not
if (mCur.moveToFirst()) {
    // you should use getCount() rather than getColumnCount()
    // getCount() is the number of rows, while getColumnCount() is the number of columns returned
    // remember your projection?? only return 1 column.
    path = new String[mCur.getCount()]; 
    int whichColumn = mCur.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    int i = 0;
    do {
        path[i] = mCur.getString(whichColumn);
        i++;
    } while (mCur.moveToNext());
}
于 2012-09-06T07:45:32.813 回答
0

你为什么使用 For 循环,使用 do whole loop as

        if (cursor.moveToFirst()){
        do{
                       // get the data from here

          }while (cursor.moveToNext());
    }

或使用 while 循环作为

                   if (mCursor != null) {
                   mCursor.moveToFirst();
                   while(!mCursor.isAfterLast()){

                        // get the data from here

                       mCursor.moveToNext();
                    }
               }
于 2012-09-06T07:41:15.703 回答