How to get songs and other media from an album?

I want to get songs and other MEDIA data from Id album. All I have is Album Id, and I tried many solutions, but none of them succeeded.

My code snippet:

ContentResolver contentResolver = getContentResolver();
        Uri mediaUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, albumId);
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);
                Log.wtf("Title", thisTitle);

                // Add the info to our array.
                songArrayList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (mediaCursor.moveToNext());

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

Magazine for the mediaUrireturn path of the current album, for example .: content://media/external/audio/media/41. Will someone tell me how to do this?

+2
source share
5 answers

I realized this after a lot of trial and error. I don’t know how much the best and safest way to do this is, but how much it works, I’m happy.

I changed my code a bit and compared album IDs. Here is a snippet:

ContentResolver contentResolver = getContentResolver();
        Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int albumId = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                long thisalbumId = mediaCursor.getLong(albumId);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);

                // Add the info to our array.
                if(this.albumId == thisalbumId)
                {
                    Log.wtf("SAME2SAME", String.valueOf(thisalbumId));
                    Log.wtf("SAME2SAME", String.valueOf(this.albumId));
                    songArrayList.add(new Song(thisId, thisTitle, thisArtist));
                }
            }
            while (mediaCursor.moveToNext());

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

I changed:

  • Albumsto Mediac MediaStore.Audio.Audio.xxx.
  • Received album Id Media.
  • album Id bundle extras.
  • arrayList. , Artists.
+1

. sqlite- "selection" contentResolver.query. , . , .

    ArrayList<Song> songs = new ArrayList<>();
    String selection = "is_music != 0";

    if (albumId > 0) {
        selection = selection + " and album_id = " + albumId;
    }

    String[] projection = {
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DISPLAY_NAME,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ALBUM_ID
    };
    final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";

    Cursor cursor = null;
    try {
        Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        cursor = getActivity().getContentResolver().query(uri, projection, selection, null, sortOrder);
        if (cursor != null) {
            cursor.moveToFirst();
            int position = 1;
            while (!cursor.isAfterLast()) {
                Song song = new Song();
                song.setTitle(cursor.getString(0));
                song.setDuration(cursor.getLong(4));
                song.setArtist(cursor.getString(1));
                song.setPath(cursor.getString(2));
                song.setPosition(position);
                song.setAlbumId(cursor.getLong(6));

                cursor.moveToNext();
            }
        }

    } catch (Exception e) {
        Log.e("Media", e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
+3
 public Bitmap getAlbumArt(Long albumId) {
        Bitmap albumArt = null;
        try {
            final Uri AlbumArtUri = Uri.parse("content://media/external/audio/albumart");
            Uri uri = ContentUris.withAppendedId(AlbumArtUri, albumId);
            ParcelFileDescriptor pfd = GlobalSongList.getInstance().getContentResolver().openFileDescriptor(uri, "r");

            if (pfd != null) {
                FileDescriptor fd = pfd.getFileDescriptor();
                albumArt = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (Exception e) {
        }
        return albumArt;
    }

ALBUM_ID - MediaStore, , , ..

    public Long getAlbumId(String id)
    {
        Cursor musicCursor;
        String[] mProjection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID};
        String[] mArgs = {id};
        musicCursor = musicResolver.query(musicUri, mProjection, MediaStore.Audio.Media._ID + " = ?", mArgs, null);
        musicCursor.moveToFirst();
        Long albumId=musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
        return albumId;
    }
0

. .

String selection= MediaStore.Audio.Media.IS_MUSIC+"!=0";
    String[] projection={
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Albums.ALBUM_ID,
            MediaStore.Audio.Media.DURATION
    };

    Cursor cursor=getActivity().managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,"title asc");
    List<String> data=new ArrayList<>();
    List<String> songs=new ArrayList<>();
    List<String> artists=new ArrayList<>();
    List<String> albums=new ArrayList<>();
    List<String> album_id=new ArrayList<>();
    List<String> durations=new ArrayList<>();

    cursor.moveToFirst();
    while (cursor.moveToNext()){
        if(cursor.getString(5).equals(mAlbumId)) {
            data.add(cursor.getString(1));
            songs.add(cursor.getString(2));
            artists.add(cursor.getString(3));
            albums.add(cursor.getString(4));
            album_id.add(mAlbumId);
            durations.add(cursor.getString(6));
        }
    }
0

when you put the cursor.movenext function in a while loop, the first row of the MediaStore database will be passed without saving the data, as the function moves the cursor and then checks that it is better to make a value instead of putting the movenext function at that time.

  try {
            Projection = new String[]{
            MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM
            , MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA,                                    MediaStore.Audio.Media.SIZE};

            Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Projection, null, null, null);

            cursor.moveToFirst();

            for (int i = 0; i < 5; i++) {
                columns[i] = cursor.getColumnIndex(Projection[i]);
            }

            Music tempMusic = new Music();

            while (next) {
                tempMusic.setName(cursor.getString(columns[0]));
                tempMusic.setAlbum(cursor.getString(columns[1]));
                tempMusic.setArtist(cursor.getString(columns[2]));
                tempMusic.setUri(cursor.getString(columns[3]));
                tempMusic.setSize(cursor.getDouble(columns[4]));
                if(!cursor.moveToNext())
                {
                    next=false;
                }
                musicss.add(tempMusic);

                tempMusic = new Music();
            }
0
source

All Articles