无法从内容提供商

时间:2016-03-08 16:05:17

标签: android android-contentprovider android-contentresolver android-audiomanager android-music-player

我正在构建一个音乐播放器,并且在logcat中多次显示歌曲。我想在listview中显示歌曲。这是我的主要活动:

public class MainActivity extends AppCompatActivity {
    List<String> list=new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            File s= Environment.getExternalStorageDirectory();
            String[] as={"is_music","title"};
            Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Cursor musicCursor = getContentResolver().query(musicUri,as, null, null,null);
            String[] a=musicCursor.getColumnNames();
            musicCursor.moveToFirst();
            Log.i ("fjfj",""+musicCursor.getCount());
            for(int i=0;i<musicCursor.getCount();i++){        
               list.add(musicCursor.getString(musicCursor.getColumnIndex("title")));
               //Log.i("position",musicCursor.getPosition()+list.get(musicCursor.getPosition()));
               musicCursor.moveToNext();        
               Log.i("column",i+" "+list.get(i));
            }

        }
    }
  

03-08 21:17:26.672 2070-2070 / com.example.android.alpha1 I / column:0   欢迎来到纽约03-08 21:17:26.676   2070-2070 / com.example.android.alpha1 I / column:1空格03-08   21:17:26.688 2070-2070 / com.example.android.alpha1 I / column:2 Style   03-08 21:17:26.688 2070-2070 / com.example.android.alpha1 I / column:3   走出森林03-08 21:17:26.692   2070-2070 / com.example.android.alpha1 I / column:4所有你必须做的事   逗留03-08 21:17:26.696 2070-2070 / com.example.android.alpha1 I /栏:   5 Shake It Off 03-08 21:17:26.696 2070-2070 / com.example.android.alpha1   我/专栏:6我希望你能03-08 21:17:26.700   2070-2070 / com.example.android.alpha1 I / column:7 Bad Blood 03-08   21:17:26.704 2070-2070 / com.example.android.alpha1 I / column:8 Wildest   Dreams 03-08 21:17:26.704 2070-2070 / com.example.android.alpha1   I / column:9你如何得到女孩03-08 21:17:26.712   2070-2070 / com.example.android.alpha1 I / column:10 This Love 03-08   21:17:26.712 2070-2070 / com.example.android.alpha1 I / column:11我知道   地点03-08 21:17:26.712 2070-2070 / com.example.android.alpha1   I / column:12 Clean 03-08 21:17:26.720   2070-2070 / com.example.android.alpha1 I / column:13 Wonderland 03-08   21:17:26.724 2070-2070 / com.example.android.alpha1 I / column:14你是   恋爱03-08 21:17:26.728 2070-2070 / com.example.android.alpha1   I / column:15 New Romantics 03-08 21:17:26.732   2070-2070 / com.example.android.alpha1 I / column:16 I Know Places(Voice   备注)03-08 21:17:26.744 2070-2070 / com.example.android.alpha1   我/专栏:17我希望你(语音备忘录)03-08 21:17:26.748   2070-2070 / com.example.android.alpha1 I / column:18空格(语音   备注)03-08 21:17:26.752 2070-2070 / com.example.android.alpha1   I / column:19欢迎来到纽约03-08 21:17:26.752   2070-2070 / com.example.android.alpha1 I /栏:20空格03-08   21:17:26.756 2070-2070 / com.example.android.alpha1 I / column:21 Style   03-08 21:17:26.760 2070-2070 / com.example.android.alpha1 I / column:22   走出森林03-08 21:17:26.764   2070-2070 / com.example.android.alpha1 I / column:23所有你必须做的事   是留03-08 21:17:26.764 2070-2070 / com.example.android.alpha1   I / column:24 Shake It Off 03-08 21:17:26.764   2070-2070 / com.example.android.alpha1 I / column:25我希望你能

1 个答案:

答案 0 :(得分:1)

这样做。 初始化ArrayList并在onCreate()中调用一个函数。

在onCreate()之前

private ArrayList<String> songArrayList;
private ListView listView;

在OnCreate()中:

songArrayList = new ArrayList();
listview = (*Find view by Id *);
getSongList();
// Now create and set adapter to listview. Use songArrayList.

功能:

public void getSongList()
{
    /**
     * All the audio files can be accessed using the below initialised musicUri.
     * And there is a cursor to iterate over each and every column.
     */
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = contentResolver.query(musicUri, null, null, null, null, null);

    // If cursor is not null
    if(musicCursor != null && musicCursor.moveToFirst())
    {
        //get Columns
        int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);


        // Store the title, id and artist name in Song Array list.
        do
        {
            String thisTitle = musicCursor.getString(titleColumn);
            // Add the info to our array.
            songArrayList.add(thisTitle);
        }
        while (musicCursor.moveToNext());

        // For best practices, close the cursor after use.
        musicCursor.close();
    }
}

这是我目前正在进行的项目的示例。希望这会有所帮助。