我试图通过滑行从Uri获取图像但是我没有得到图像。
模型类 - 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);
}
}}
片段类 - 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();
}
}}
虽然我在BindViewHolder中注释了位图部分,因为当我使用该方法时,它会使recyclelerView变慢。 因此试图使用滑行,但我不知道为什么我没有获得图像。任何帮助都是宝贵的。!
答案 0 :(得分:0)
用于简单加载
Glide.with(this).load("uri")
.placeholder(R.mipmap.ic_launcher)
.into(logoIv);
使用滑行圆形图像视图
Glide.with(this)
.load("uri")
.centerCrop()
.placeholder(R.mipmap.ic_launcher)
.crossFade()
.into(logoIv);
答案 1 :(得分:0)
Glide.with(this).load(uri.getPath())
.placeholder(R.mipmap.ic_launcher)
.into(logoIv);
uri.getPath()
应解决问题。
答案 2 :(得分:0)
试试这个。
Glide.with(mContext)
.load(new File(pictureUri.getPath())) // Uri of the picture
.into(logoIv);
答案 3 :(得分:0)
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);
答案 4 :(得分:0)
最后我得到了答案 -
Glide.with(context).load(list.get(position).imageId).
placeholder(R.drawable.ic_action_movie).into(holder.imageView);