Issue
I'm trying to get Image from Uri using glide but what i'm not getting an Image.
Model class - AlbumData
public class AlbumData {
private long id;
public String title;
public String description;
public Uri imageId;
public long getID() {
return id;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
/*public int getImageId() {
return imageId;
}
*/
public Uri getArtwork(){return imageId;}
public Bitmap getArtworkBitmap(Context ctx) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(ctx.getContentResolver(), imageId);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
public AlbumData(long id, String title, String description, Uri imageId) {
this.id = id;
this.title = title;
this.description = description;
this.imageId = imageId;
}}
RecyclerView - AlbumRecyclerViewAdapter
public class AlbumRecyclerViewAdapter extends RecyclerView.Adapter<AlbumRecyclerViewAdapter.CustomViewHolder> {
private final String TAG = "test1";
Songs myTab;
MediaPlayer mediaPlayer;
public List<AlbumData> list = Collections.emptyList();
Context context;
public AlbumRecyclerViewAdapter(List<AlbumData> list, Context context, Songs myTab) {
this.list = list;
this.context = context;
this.myTab = myTab;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View songView = LayoutInflater.from(parent.getContext()).inflate(R.layout.album_custom_row, parent, false);
CustomViewHolder myHolder = new CustomViewHolder(songView, context, list);
return myHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
AlbumData album = list.get(position);
//Use the provided View Holder on the onCreateViewHolder method to
//Populate the current row on the RecyclerView
holder.getItemId();
holder.title.setText(list.get(position).title);
holder.description.setText(list.get(position).description);
//holder.imageView.setImageResource(list.get(position).imageId);
Bitmap bitmap = null;
/*try {
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), album.getArtwork());
holder.imageView.setImageBitmap(bitmap);
} catch (Exception exception) {
// log error
}
*/
Glide.with(context).load(Uri.parse("file://" + album.getArtwork())).asBitmap().skipMemoryCache(true).into(holder.imageView);
}
@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will displaye
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
//Insert a new item to the RecyclerView on a predefined position
public void insert(int position, AlbumData data) {
list.add(position, data);
notifyItemChanged(position);
}
public void remove(AlbumData data) {
int position = list.indexOf(data);
list.remove(position);
notifyItemRemoved(position);
}
public static class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CardView cv;
TextView title;
TextView description;
ImageView imageView;
Context context;
List<AlbumData> list = new ArrayList<AlbumData>();
public CustomViewHolder(View itemView, Context context, List<AlbumData> list)
{
super(itemView);
this.context = context;
this.list = list;
this.itemView.setOnClickListener(this);
cv = (CardView) itemView.findViewById(R.id.cardView);
title = (TextView) itemView.findViewById(R.id.playList_name);
description = (TextView) itemView.findViewById(R.id.album_artist);
imageView = (ImageView) itemView.findViewById(R.id.imageView1);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
AlbumData list = this.list.get(position);
}
}}
Fragment class - SongFragment
public class Songs extends Fragment {
final public static Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
private MediaPlayer mp;
private Intent playIntent;
private boolean musicBound = false;
private static final String KEY_LAYOUT_MANAGER = "layoutManager";
private RecyclerView mRecyclerView;
private AlbumRecyclerViewAdapter mAdapter;
protected List<AlbumData> songsList;
private enum LayoutManagerType {
GRID_LAYOUT_MANAGER,
LINEAR_LAYOUT_MANAGER
}
protected LayoutManagerType mCurrentLayoutManagerType;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize dataset, this data would usually come from a local content provider or
// remote server.
//fill_with_data();
//Instantiate the list
songsList = new ArrayList<AlbumData>();
getSongList();
Collections.sort(songsList, new Comparator<AlbumData>() {
public int compare(AlbumData a, AlbumData b) {
return a.getTitle().compareTo(b.getTitle());
}
});
}
public Songs() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_songs, container,
false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
if (savedInstanceState != null) {
// Restore saved layout manager type.
mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
.getSerializable(KEY_LAYOUT_MANAGER);
}
mAdapter = new AlbumRecyclerViewAdapter(songsList, getContext(), Songs.this);
// Set CustomAdapter as the adapter for RecyclerView.
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = MediaStore.Audio.Albums.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.Albums.ALBUM);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Albums._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Albums.ARTIST);
//int imageColumn = musicCursor.getColumnIndex
// (MediaStore.Audio.Albums.ALBUM_ART);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
//int thisAlbum = (int) musicCursor.getLong(imageColumn);
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, thisId);
songsList.add(new AlbumData(thisId, thisTitle, thisArtist, albumArtUri));
}
while (musicCursor.moveToNext());
musicCursor.close();
}
}}
Though I commented out the bitmap section in BindViewHolder as when i using that method, it makes recyclerView slow. So trying to use glide but I don't know why i'm not getting image. Any help would be precious.!
Solution
You are using Uri in ImageId, get string from ImageId like below, hope it will work
Glide.with(this)
.load(list.get(position).imageId)
.centerCrop()
.placeholder(R.mipmap.ic_launcher)
.crossFade()
.into(logoIv);
Answered By - Saqib Khalil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.