0

I want to display album art with song name in a listview with the help of Custom ArrayAdapter.

I have tried the below code, the song name and artist name is displaying perfectly but not the albumArt.I have tried using Bitmap to decode the images but no luck.

Here is my ListSong class which is used to fetch songs using MediaStore.

package com.example.adityaverma.practicemusic;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;

/**
 * Created by Aditya Verma on 17-10-2017.
 */

public class ListSong extends Fragment {

    private ArrayList<Song> songList;
    private ListView songView;
    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.listsongs, container, false);

        songView = (ListView) rootView.findViewById(R.id.songsList);
        songList = new ArrayList<Song>();

        getSongList();

        SongAdapter songAdt = new SongAdapter(getActivity(), songList);
        songView.setAdapter(songAdt);

        return rootView;

    }

    //This method is used to retrieve the songs from external storage of the device and adding them into our listview.

    public void getSongList() {
        ContentResolver musicResolver = getActivity().getContentResolver();
        Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
        if (musicCursor != null && musicCursor.moveToFirst()) {
            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ARTIST);
            int albumid = musicCursor.getInt
                    (musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

            Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumid);

            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(
                        context.getContentResolver(), albumArtUri);
                bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);

            } catch (FileNotFoundException exception) {
                exception.printStackTrace();
                bitmap = BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.);
            } catch (IOException e) {

                e.printStackTrace();
            }


            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                long thisalbumid = musicCursor.getLong(albumid);

                songList.add(new Song(thisId, thisTitle, thisArtist , thisalbumid));
            }
            while (musicCursor.moveToNext());
            musicCursor.close();
        }

    }

}

Here is my bean class which is used to store the SongInfo.

package com.example.adityaverma.practicemusic;

public class Song {

    private long id;
    private String title;
    private String artist;
    private long albumId;

    public Song(long songID, String songTitle, String songArtist , long songalbumId) {
        this.id = songID;
        this.title = songTitle;
        this.artist = songArtist;
        this.albumId = songalbumId;
    }

    public long getID(){return id;}
    public String getTitle(){return title;}
    public String getArtist(){return artist;}
    public long getAlbumId(){return albumId;}

}

And finally here is my SongAdapter which is used to bind my ListView with the ListSong class.This is where I think is the problem.

package com.example.adityaverma.practicemusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class SongAdapter extends BaseAdapter {

    private ArrayList<Song> songs;
    private LayoutInflater songInf;

    public SongAdapter(Context c, ArrayList<Song> theSongs) {
        songs = theSongs;
        songInf = LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //map to song layout
        LinearLayout songLay = (LinearLayout) songInf.inflate
                (R.layout.songviewer, parent, false);
        //get title and artist views
        TextView songView = (TextView) songLay.findViewById(R.id.song_title);
        TextView artistView = (TextView) songLay.findViewById(R.id.song_artist);
        ImageView albumArtView = (ImageView) songLay.findViewById(R.id.albumArtSongs);
        //get song using position
        Song currSong = songs.get(position);
        //get title and artist strings
        songView.setText(currSong.getTitle());
        artistView.setText(currSong.getArtist());
        //set position as tag
        songLay.setTag(position);
        return songLay;
    }

}

I just want to display my albumArt with the correct songName and artist name.

NOTE: I am just a beginner in this things so please kindly elaborate your answer and point out where i was wrong. And might give an explanation.

K.Os
  • 5,123
  • 8
  • 40
  • 95
Aditya Verma
  • 120
  • 2
  • 10
  • well, for sure that return null; inside getItem() method is not helping you...Replace it returning appropriate song, like songs[i] – Shine Nov 06 '17 at 09:51
  • also, before inflating view, you should check if passed convertVieew is null. If not, reuse it instead of creating a new one each time. – Shine Nov 06 '17 at 09:58

0 Answers0