Get AlbumArt and TrackName using ListView in Android
The below method shows to create list of AlbumArt and TrackName using ListView in Android.
MainActivity.java
public class MainActivity extends AppCompatActivity {
List<String> Albumid=new ArrayList<>();
List<String> SongName=new ArrayList<>();
ListView listView;
String Album_id[],SongNames[];
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.lists);
getAlbumArt();
if(Albumid!=null){
//converting List<String> to Arrays[]
Album_id=Albumid.toArray(new String[Albumid.size()]);}
else{
Log.d("Error","List string is null");
}
getSong();
if(SongName!=null){
SongNames=SongName.toArray(new String[SongName.size()]);
}
else{
Log.d("Error","Songtrack is empty");
}
CustomizedListAdapter adapter=new CustomizedListAdapter(this,null,Album_id,SongNames);
listView.setAdapter(adapter);
getSelectedPath();
}
public void getSong(){
try{
ContentResolver c=getContentResolver();
Uri uri=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor1=c.query(uri,null,MediaStore.Audio.Media.IS_MUSIC+ "=1",null,null);
if(cursor1 !=null && cursor1.moveToFirst()){
int songname=cursor1.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
do{
String songs=cursor1.getString(songname);
SongName.add(songs);
}while(cursor1.moveToNext());
}
}catch(NumberFormatException e){
e.printStackTrace();
}
}
public void getAlbumArt() {
try {
ContentResolver cr = getContentResolver();
Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
Cursor cursor = cr.query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int albumart = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
do {
String Albumids = cursor.getString(albumart);
Albumid.add(Albumids);
} while (cursor.moveToNext());
}cursor.close();
} catch (NumberFormatException e){
e.printStackTrace();
}
}
public void getSelectedPath(){
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView adapterView, View view, int i, long l) {
String path= String.valueOf(listView.getItemAtPosition(i));
if(path.equals("null")){
ImageView imgv=(ImageView)view.findViewById(R.id.imageView);
imgv.setImageResource(R.drawable.unknowalbum);
imgv.setMaxHeight(50);
imgv.setMaxWidth(50);
}
else{
Drawable image=Drawable.createFromPath(path);
ImageView imgview=(ImageView)view.findViewById(R.id.imageView);
imgview.setImageDrawable(image);
}
}
});
}
}
CustomizedListAdapter.java public class CustomizedListAdapter extends ArrayAdapter {
private Activity context;
private Integer Images[];
private String SongName[];
private String Path[];
public CustomizedListAdapter(Activity context,Integer Images[],String Path[],String SongName[]){
super(context,R.layout.custom,Path);
this.context=context;
this.Images=Images;
this.Path=Path;
this.SongName=SongName;
}
private class ViewHolder{
ImageView imgview;
TextView txtview;
TextView songtxt;
}
@NonNull @Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater=context.getLayoutInflater();
if(convertView==null){
convertView=inflater.inflate(R.layout.custom,null);
holder=new ViewHolder();
holder.imgview=(ImageView)convertView.findViewById(R.id.imageView);
holder.txtview=(TextView)convertView.findViewById(R.id.textView);
holder.songtxt=(TextView)convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else{
holder=(ViewHolder)convertView.getTag();
}
holder.imgview.setImageResource(R.mipmap.ic_launcher);
holder.txtview.setText(Path[position]);
holder.songtxt.setText(SongName[position]);
return convertView;
}
}
Get the below permission in manifest file...
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
public class MainActivity extends AppCompatActivity {
ReplyDeleteListAlbumid=new ArrayList<>();
ListSongName=new ArrayList<>();
in it ListAlbumid & ListSongName is shown error, please tell me how we can remove these error. please tell me , i m stuck on it.
My bad it is List Albumid=new ArrayList<>(); and List SongName=new ArrayList<>();.....There copy pasting error.....
Delete