0

album art with cursor and base adapter not showing in list or toast

i am trying to display the album art and album details in a listview however neither does it display in toast or in listview

here is the code

mainactivity

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //retrieveAudioFiles();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressLint("NewApi")
    public void retrieveAudioFiles(){
        SongsList songsList = new SongsList();

        //Uri sd = Audio.Media.INTERNAL_CONTENT_URI ;
        //Uri sd = Audio.Media.EXTERNAL_CONTENT_URI;
        Uri sd = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        String[] cols = {Audio.Media.TITLE,Audio.Media.ARTIST,Audio.Media.ALBUM};
        String where = Audio.Media.IS_MUSIC;
        Cursor audioCursor = getContentResolver().query(sd,cols,where,null,null);

        while (audioCursor.moveToNext()){
            int posColTitle = audioCursor.getColumnIndex(Audio.Media.TITLE);
            int posColArtist = audioCursor.getColumnIndex(Audio.Media.ARTIST);
            int posColAlbum = audioCursor.getColumnIndex(Audio.Media.ALBUM);

            String songTitle = audioCursor.getString(posColTitle);
            String songArtist = audioCursor.getString(posColArtist);
            String songAlbum = audioCursor.getString(posColAlbum);
            int posColId = audioCursor.getColumnIndex(Audio.Media._ID);
            long songId = audioCursor.getLong(posColId);
            Uri songUri = ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI,songId);
            String[] dataColumn = {Audio.Media.DATA};
            Cursor coverCursor = getContentResolver().query(songUri, dataColumn, null, null, null);
            coverCursor.moveToFirst();
            int dataIndex = coverCursor.getColumnIndex(Audio.Media.DATA);
            String filePath = coverCursor.getString(dataIndex);
            coverCursor.close();
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(filePath);
            byte[] coverBytes = retriever.getEmbeddedPicture();
            Bitmap songCover;
            Toast.makeText(getApplicationContext(),audioCursor.getString(audioCursor
                    .getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM)), 
                   Toast.LENGTH_LONG).show();

            if (coverBytes!=null) //se l'array di byte non è vuoto, crea una bitmap
                songCover = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
            else
                songCover=null;
            songsList.add(new Song(songTitle,songArtist,songAlbum));
            }
        audioCursor.close();
    }
    public class SongsAdapter extends BaseAdapter {

        private SongsList songsList;
        private LayoutInflater songInf;

        public SongsAdapter(Context c, SongsList theSongs){
            super();  
            songsList=theSongs;
            songInf=LayoutInflater.from(c);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return songsList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return songsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            RowWrapper wrapper;
            if (convertView == null)
            {
                convertView = songInf.inflate(
                    R.layout.song_row, null);
                wrapper = new RowWrapper(convertView);
                convertView.setTag(wrapper);
            }
            else
            {
                wrapper = (RowWrapper) convertView.getTag();
            }
            Song song = (Song) getItem(position);
            wrapper.populate(song);

            return convertView;
        }

        private class RowWrapper
        {
            private TextView titleTextView;
            private TextView artistTextView;
            private TextView albumTextView;
            private ImageView coverImageView;

            public RowWrapper(View convertView)
            {
                titleTextView = (TextView) convertView.findViewById(R.id.textTitle);
                artistTextView = (TextView) convertView.findViewById(R.id.textArtist);
                albumTextView = (TextView) convertView.findViewById(R.id.textAlbum);
                coverImageView = (ImageView) convertView.findViewById(R.id.smallCover);
            }

            public void populate(Song song)
            {
                titleTextView.setText(song.title);
                artistTextView.setText(song.artist);
                albumTextView.setText(song.album);
               if (song.cover != null)
                coverImageView.setImageBitmap(song.cover);
            }
        }

    }
}

then in the song class

public class Song {

    public String title="";
    public String artist="";
    public String album="";
    public Bitmap cover=null;

    public Song(String t, String ar, String al){
        title=t;
        artist=ar;
        album=al;
        //cover=c;
    }

    public Song(){

    }

}

and songList class

public class SongsList extends ArrayList<Song> {

    public SongsList(){
        super();
    }

}

how ever nothing is displayed

here are the xml files

activty_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.stackalbumart.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

and song_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:onClick="songPicked" >

    <ImageView
        android:id="@+id/smallCover"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="coverDescription"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="labelTitle" />

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="textTitle" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelArtist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:text="labelArtist" />

        <TextView
            android:id="@+id/textArtist"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="textArtist" />

        <TextView
            android:id="@+id/labelAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="labelAlbum" />

        <TextView
            android:id="@+id/textAlbum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="textAlbum" />
    </LinearLayout>

</LinearLayout>

the app just opens and there is a blank white screen there is even no error

what really should be done

1234567
  • 2,226
  • 4
  • 24
  • 69
  • you have `audioCursor` so why dont you use a `SimpleCursorAdapter` ? – pskink Jun 29 '15 at 07:31
  • i am currently trying to learn android and this is a task i have to do, use cursor and base adapter with album art how can it be done in above code – 1234567 Jun 29 '15 at 07:33
  • if you want to learn android then use `SimpleCursorAdapter` and not write hundreds lines of redundant code... – pskink Jun 29 '15 at 07:37
  • is there an example i can refer to where SimpleCursorAdapter is used to display album art from mp3 files to be displayed in listview. – 1234567 Jun 29 '15 at 07:39
  • there are tons of examples on how to use `SimpleCursorAdapter`, just ask uncle google – pskink Jun 29 '15 at 07:40

1 Answers1

0

Since you are handle cursor, I would strongly suggest you to subclass CursorAdapter, which extends BaseAdapter and has the additional logic to handle the Cursor.

About your issue

in your retrieveAudioFiles(), after you close your cursor, you should instantiate the adapter and feed it to your ListView. E.g.

ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new SongsAdapter(this, songList));
Blackbelt
  • 156,034
  • 29
  • 297
  • 305