Android songs fetching from SD card

后端 未结 5 661
执念已碎
执念已碎 2020-12-06 03:37

I am fetching my songs from the SD card and putting him to the list view.

I am using this method. but its taking some time and if path is different I didn\'t get tha

5条回答
  •  醉梦人生
    2020-12-06 04:22

    ArrayList names = new ArrayList();
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
        Uri uri;
        int columnIndex;
        String[] projection = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ALBUM_ID };
        ListView lst;
        ArrayAdapter adapter;
        Cursor cursor;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
    
            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    
            cursor = this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    projection, selection, null, null);
    
            if (cursor.getCount() == 0) {
    
                Toast.makeText(getBaseContext(),
                        "cursor value" + cursor.getCount(), Toast.LENGTH_SHORT)
                        .show();
            } else {
    
                cursor.moveToFirst();
                do {
                    names.add(cursor.getString(cursor
                            .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
    
                } while (cursor.moveToNext());
    
                adapter = new ArrayAdapter(Home.this,
                        android.R.layout.simple_list_item_1, names);
    
                lst.setAdapter(adapter);
            }
    

    I am getting cursor count 0 actually i added a folder to sdcard but files are not coming

提交回复
热议问题