我的代码:holder.icon.setImageResource(current.imageUrl);这里imageUrl已在String中声明。但是setImageResource只接受int。任何人都可以为我提供如何获得字符串的解决方案,还是有其他方法可用于它?
答案 0 :(得分:2)
我想你是从互联网上取得的。
private Bitmap getBitMapFromUrl( String imageuri){
HttpURLConnection connection=null;
try {
URL url=new URL(imageuri);
connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is=connection.getInputStream();
Bitmap mybitmap=BitmapFactory.decodeStream(is);
return mybitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}}
传递字符串值并返回位图。
holder.icon.setImageBitmap(getBitmapFromUrl());
答案 1 :(得分:1)
您正在使用“setImage 资源”!
它需要一个Resource(通常是一个可绘制的资源),因此需要int。
@Mayuri Joshi建议的下载解决方案可能符合您的需求,如果没有,请提供有关您要完成的内容的更多信息:)答案 2 :(得分:0)
You have to download the image firstly
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
Then use the Imageview.setImageBitmap to set bitmap into the ImageView