相册艺术未显示

时间:2018-01-13 06:26:53

标签: android nullpointerexception android-mediaplayer

我试图获得专辑封面,但专辑封面并没有出现在应用程序运行时。我不知道whts wrog with this.please帮我解决这个问题。在互联网上找到了很多解决方案但是无法得到这个。 这是我的日志。

  

没有连接适配器;跳过布局01-11 13:46:27.310   809-809 / com.example.murarilal.atry I / Choreographer:跳过51帧!   应用程序可能在其主线程上做了太多工作。 01-11   13:46:27.348 809-840 / com.example.murarilal.atry D / EGL_emulation:   eglMakeCurrent:0xa3b850c0:ver 2 0(tinfo 0xa3b831e0)01

  private void loadSongs() {
            Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
             cursor = getContext().getContentResolver().query(uri, null, selection, null, null);
            if (cursor != null) {
                int idColumn = cursor.getColumnIndex
                        (android.provider.MediaStore.Audio.Media._ID);
                if (cursor.moveToFirst()) {
                    do {

                        String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                        String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                        String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                        long albumId = cursor.getLong(idColumn);
                        songInfo s = new songInfo(name, artist, url,albumId);
                        _songs.add(s);
                    }
                    while (cursor.moveToNext());
                    cursor.close();
                    recyclerView.getAdapter().notifyDataSetChanged();
                }
            }
        }

public class songInfo {
    private String SongName;
    private String ArtistName;
    private String SongUrl;
    private long album_art;

    public songInfo() {
    }

    public songInfo(String songname, String artistname, String songUrl, long album) {
        SongName = songname;
        ArtistName = artistname;
        SongUrl = songUrl;
        album_art=album;
    }

    public String getSongName() {
        return SongName;
    }

    public void setSongName(String songName) {
        SongName = songName;
    }

    public String getArtistName() {
        return ArtistName;
    }

    public void setArtistName(String artistName) {
        ArtistName = artistName;
    }

    public String getSongUrl() {
        return SongUrl;
    }
    public long getAlbum_art() {
        return album_art;
    }



    public void setSongUrl(String songUrl) {
        SongUrl = songUrl;
    }

}



 my adapter



public  class songAdapter extends RecyclerView.Adapter<songAdapter.SongHolder>{

    private ArrayList<songInfo> _songs = new ArrayList<>();
    private Context context;
    Cursor cursorm;

    private OnItemClickListener mOnItemClickListener;

    public songAdapter(Context context, ArrayList<songInfo> songs) {
        this.context = context;
        this._songs = songs;
    }


    public interface OnItemClickListener {
        void onItemClick(RecyclerView.ViewHolder holder, View view, songInfo obj, int position);

        // void onItemClick(Button b, View view, songInfo obj, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mOnItemClickListener = mItemClickListener;
    }


    @Override
    public SongHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View myView = LayoutInflater.from(context).inflate(R.layout.card,viewGroup,false);
        return new SongHolder(myView);
    }

    @Override
    public void onBindViewHolder(final SongHolder songHolder, final int i) {
        final songInfo s = _songs.get(i);
       /* cursorm =context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                MediaStore.Audio.Albums._ID+ "=?",
                new String[] {String.valueOf(albumId)},
                null);
        String albumId = cursorm.getString(cursorm.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

        if (cursorm != null && cursorm.moveToFirst()) {
            String path = cursorm.getString(cursorm.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
            Drawable img = Drawable.createFromPath(path);
            songHolder.album_art.setImageDrawable(img);
        }*/
        songHolder.album_art.setImageDrawable(Drawable.createFromPath(String.valueOf(_songs.get(i).getAlbum_art())));
        songHolder.tvSongName.setText(_songs.get(i).getSongName());
        songHolder.tvSongArtist.setText(_songs.get(i).getArtistName());
        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, View view, songInfo obj, int position) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(songHolder, view, s, i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return _songs.size();
    }

    public class SongHolder extends RecyclerView.ViewHolder {
        TextView tvSongName,tvSongArtist;
        ImageView album_art;
        public SongHolder(View itemView) {
            super(itemView);
            tvSongName = (TextView) itemView.findViewById(R.id.songName);
            tvSongArtist = (TextView) itemView.findViewById(R.id.artistName);
            album_art = (ImageView) itemView.findViewById(R.id.albumArt);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

此行上有空指针异常:

String albumId = cursorm.getString(cursorm.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

您的Cursor cursorm之前从未初始化过。您正在上面一行之后初始化cursor。检查cursorm变量的初始化。

答案 1 :(得分:0)

因为在初始化之前使用cursorm而发生错误。在将其设置到下一行之前,请调用此行。

String albumId = cursorm.getString(cursorm.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

P.S.-形成我的理解你正试图展示每首歌的专辑艺术。为此,在SongInfo中存储albumId并从ArrayList获取它,而不是从MediaStore中为每首歌曲收集albumId。