Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch Genre name list which have songs in it

I am fetching Genre list from media content Provider of android using CursorLoder class. below is my cursor query to fetch the list of Genre.

 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // currently filtering.
        Uri baseUri;
        baseUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
        String[] STAR = { "*" };
        return new CursorLoader(getActivity(), baseUri, STAR, null, null, null );
    }

Now, i got all the genre list from the media content provider but the problem with the data i got. I also got the genre name which is created before but right now i don't have song in it.

I only want the genre that have songs in it, not the genre name that dont have songs in it.

Can anyone help me in that?

UPDATE

I can able to fetch all the Genre and its Song using below code...

private void getGenresList() {

        //-------------------------------------

        int index;
        long genreId;
        int count;
        Uri uri;
        Cursor genrecursor;
        Cursor tempcursor;
        String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};   
        String[] proj2={MediaStore.Audio.Media.DISPLAY_NAME};

         genrecursor=getActivity().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,proj1,null, null, null);
         if(genrecursor.moveToFirst())
         {
             do{
                index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);            
                System.out.println("GENRE NAME: "+genrecursor.getString(index));
                System.out.println("======================================");

                index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);             
                genreId=Long.parseLong(genrecursor.getString(index));
                uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

                tempcursor = getActivity().getContentResolver().query(uri, proj2, null,null,null);
                System.out.println("Total Songs: "+tempcursor.getCount());
                if(tempcursor.moveToFirst())
                {
                    do{
                        index=tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                        System.out.println("    Song Name: "+tempcursor.getString(index));

                    }while(tempcursor.moveToNext());
                }
                System.out.println("======================================");
             }while(genrecursor.moveToNext());       
         }


        //-------------------------------------

    }

But, i must have to use two query, one for fetching all the genre and second for filter data with the Genre which have songs. Even i would like to use that in the CursorLoader so i am wonder wonder about how to do that.

Hope you got proper idea what i want with this question.

like image 442
Shreyash Mahajan Avatar asked Aug 26 '14 04:08

Shreyash Mahajan


1 Answers

It seems like there is no other method to achieve this so i have just do like below... With below update i can able to get the Genre list which only have songs in it. The genre list which don't have songs are filtered out.

public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        Uri baseUri;
        if (mCurFilter != null) {
            baseUri = Uri.withAppendedPath(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    Uri.encode(mCurFilter));
        } else {
            baseUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
        }

        String[] STAR = { MediaStore.Audio.Genres._ID,
                MediaStore.Audio.Genres.NAME };


        String query = " _id in (select genre_id from audio_genres_map where audio_id in (select _id from audio_meta where is_music != 0))" ;

        return new CursorLoader(getActivity(),
                MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, STAR, query,
                null, null);

    }

In above code i have join the genre table with audio table. with this way i am able to fetch, whether that Genre have songs or not. So now i have only that genre which have songs in it.

Hope this will help others also.

Enjoy Coding... :)

like image 149
Shreyash Mahajan Avatar answered Sep 22 '22 01:09

Shreyash Mahajan