1

我正在使用以下代码获取设备上的歌曲列表。最终我想用它们做更多的事情,但刚开始我想在 Android 设备中找到音频/音乐。

我已经使用此代码来查询媒体存储并且我一直得到一个空游标...我已经检查了这些堆栈溢出答案,但它们要么不相关,要么我对它们的理解不足以实现它们...

将不胜感激任何帮助!提前致谢

public class AudioFinalActivity extends Activity {

    private TextView tv;
    private String res;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // link text view obj
        tv = (TextView) findViewById(R.id.tv);
        res = "";

         String[] proj = { MediaStore.Audio.Media._ID,
         MediaStore.Audio.Media.DATA,
         MediaStore.Audio.Media.DISPLAY_NAME,
         MediaStore.Audio.Artists.ARTIST };
         // managed query doesn't need startManagingCursor called on the
         Cursor c = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
         proj, null, null, null);

//      ContentResolver contentResolver = getContentResolver();
//      String[] columns = { MediaColumns.TITLE, AudioColumns.DURATION,
//              MediaColumns.DATA
//      // add more columns if you want to fetch more data
//      };
//
//      Cursor c = contentResolver.query(
//              MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, null,
//              null, null);

        if (c != null) {
            Log.d("AFA", "Cursor returned NULL");
        } else if (c.getCount() < 1) {
            Log.d("AFA", "Cursor query is empty.. :( ...");
        } else {
            // do stuff with our content...
            while (c.moveToNext()) {

                //String title = c.getString(c
                        .getColumnIndex(MediaColumns.TITLE));
                //Long duration = c.getLong(c
                        .getColumnIndex(AudioColumns.DURATION));
                //String data = c.getString(c
                        .getColumnIndex(MediaColumns.DATA));

                //res += title + "\n";
                res +=  c.getString(c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)) + "\n";
                tv.setText(res);
            }
        }
    }
}

我股票音乐播放器可以很好地播放我的手机音频..

编辑:

我刚刚删除了对光标为空和光标为空的检查,我似乎得到了一个结果..奇怪为什么光标为空而 while(c.MoveToNext())... 返回值...嗯

4

1 回答 1

1

your checking is if (c != null) {}, but I suppose it is if (c == null) {} (if you are checking the cursor being null)

于 2012-08-06T09:36:11.227 回答