我从我的服务器获取图像网址列表,并使用适配器将它们设置在recyclerview中。我的要求是从图像中获取颜色并将该颜色设置为textview的背景,因为我使用调色板库如下。但我得到错误InputStream行无法排序的问题,任何帮助将不胜感激。无法将图像网址转换为位图
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Album albm=malbumlist.get(position);
holder.title.setText(albm.getProductName());
System.out.println("Displaying the image on Recycleview");
Glide.with(mcontext).load(albm.getImageUrl()).into(holder.thumbnail);
try {
URL url=new URL(albm.getImageUrl());
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is=connection.getInputStream(); /* Getting error here */
bmp=BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
int bgcolor=palette.getVibrantColor(mcontext.getResources().getColor(android.R.color.black));
holder.linearLayout.setBackgroundColor(bgcolor);
}
});
}
当我在我的onBindviewholder方法中使用相同的方法时,它将错误显示为java.lang.IllegalArgumentException:YOu必须在后台线程上调用此方法 – user2645941
我刚刚编辑了我的答案,请检查Glide第二个示例。 –