如何为我的应用中的歌曲设置专辑封面?

时间:2017-01-28 14:46:28

标签: java android xml

我尝试了各种解决方案将图片加载到我的应用中。代码如下。

MainActivity.java

    public class MainActivity extends AppCompatActivity implements MediaController.MediaPlayerControl
    {
    private ArrayList<Song> songArrayList;
    private ListView songView;
    private int res = 23;
    private musicservice musicSrv;
    private Intent playIntent;
    private boolean musicBound=false;
    private MusicController controller;
    private boolean paused=false, playbackPaused=false;
    Button songsort,artsort;
    int acheck=0,scheck=0,dcheck=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!isReadStorageAllowed())
        {
            requestStoragePermission();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        songView = (ListView) findViewById(R.id.song_list);
        songArrayList = new ArrayList<Song>();
        getSongList();
        songsort = (Button) findViewById(R.id.SongSortButton);
        artsort = (Button) findViewById(R.id.ArtistSortButton);
        ActionBar bar = getSupportActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
        SongAdapter songAdt = new SongAdapter(this, songArrayList);
        songView.setAdapter(songAdt);
        setController();
    }

    public Bitmap getAlbumart(Long album_id)
        {
        Bitmap bm = null;
        try
        {
            final Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");

            Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
            ParcelFileDescriptor pfd =  MainActivity.this.getContentResolver().openFileDescriptor(uri, "r");

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

    public void getSongList()
        {
            ContentResolver musicResolver = getContentResolver();
            Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Cursor musicCursor = musicResolver.query(musicUri,null,null,null,null);
        if(musicCursor!=null && musicCursor.moveToFirst()){
            int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
            int album = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
            int dateAdded = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED);
            int dateModified = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATE_MODIFIED);
            int albumID = musicCursor.getColumnIndex(MediaStore.Audio.Albums._ID);
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                String thisDateMod = musicCursor.getString(dateModified);
                String thisDateAdd = musicCursor.getString(dateAdded);
                String thisalbum = musicCursor.getString(album);
                Long thisAlbumID = musicCursor.getLong(albumID);
                songArrayList.add(new Song(thisId, thisTitle, thisArtist, thisalbum, thisDateAdd, thisDateMod,thisAlbumID));
            }
            while (musicCursor.moveToNext());
        }
}

SongAdapter.java

    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 position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout songLay = (LinearLayout)songInf.inflate(R.layout.song, parent, false);
        TextView songView = (TextView)songLay.findViewById(R.id.song_title);
        TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
        ImageView albumArt = (ImageView)songLay.findViewById(R.id.albumArt);
        Song currSong = songs.get(position);
        songView.setText(currSong.getTitle());
        artistView.setText(currSong.getArtist());
        MainActivity mainActivity = new MainActivity();
        Long id = currSong.getAlbID();
        Log.d("data",Long.toString(id));
        albumArt.setImageBitmap(mainActivity.getAlbumart(id));
        songLay.setTag(position);
        return songLay;
    }

    }

Song.java

public class Song {
private long id;
private String title,artist,dateadd,datemod,album;
Long albID;

public Song(long songID, String songTitle, String songArtist, String albumname, String dateAdded, String dateModified, Long albumID) {
    id=songID;
    title=songTitle;
    artist=songArtist;
    dateadd=dateAdded;
    datemod=dateModified;
    album=albumname;
    albID=albumID;
}
public long getID(){return id;}
public String getTitle(){return title;}
public String getArtist(){return artist;}
public String getDateadd(){return dateadd;}
public String getAlbum(){return album;}
public String getDatemod(){return datemod;}
public Long getAlbID(){return albID;}
}

song.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:onClick="songPicked">

    <ImageView
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:id="@+id/albumArt"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80sp"
        android:orientation="vertical">
    <TextView
        android:paddingTop="10sp"
        android:id="@+id/song_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/song_artist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:paddingBottom="10sp"/>
    </LinearLayout>
</LinearLayout>

activty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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"
    android:orientation="vertical"
    tools:context="com.example.rahulvarma.musicplayer.MainActivity"
    android:background="@drawable/m"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50sp"
        android:orientation="horizontal"
        android:paddingLeft="40sp">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Song"
        android:layout_gravity="center"
        android:id="@+id/SongSortButton"
        android:onClick="reordersong"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Artist"
        android:id="@+id/ArtistSortButton"
        android:onClick="reorderartist"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Date"
        android:id="@+id/DateSortButton"
        android:onClick="reorderdate"/>
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/song_list">
    </ListView>
</LinearLayout>

我试过运行应用程序,它始终显示没有图像。我不知道我做错了什么。

0 个答案:

没有答案