My code: holder.icon.setImageResource(current.imageUrl); here the imageUrl is been declared in String. But setImageResource takes only int. Can anyone provide me a solution how to get a string or is there anyother method available for it?
Asked
Active
Viewed 1,479 times
1
-
1Use UIL(Universal Image Loader) to load image using url. – Vibhor Chopra Oct 31 '15 at 07:18
-
you can also use picasso for that...https://github.com/square/picasso – Das Oct 31 '15 at 07:19
-
I'm not allowed to use third part Libraries. – Sayyaf Oct 31 '15 at 07:31
3 Answers
3
I think u are fetch the image from internet.
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;
}}
pass the string value and return the bitmap.
holder.icon.setImageBitmap(getBitmapFromUrl());
-
This no longer works in Kotlin. NetworkOnMainThreadException will occur – software is fun Nov 18 '20 at 04:45
1
you are using the "setImageResource" !
it expects a Resource (usually a drawable resource), hence the int requirement.
the download solution suggested by @Mayuri Joshi might fit your needs, if not, please provide more information regarding what it is you are trying to accomplish :)

JozeRi
- 3,219
- 6
- 27
- 45
-
I have 3 classes one is the main activity,2nd class is for My Adaper,3rd is only ItemData Class which holds String value say, String imageurl. Im using String Url in main activity to get array of URLs in imageurl. then wants the same imageurl in my adapter class to setImageResource for it. but thats not logical. :P I have to convert my Image in to bitmap pass in setImageBitmap. – Sayyaf Oct 31 '15 at 07:36
-
Hope I'm right? Is it possible to write the Bitmap function in OnBindViewHolder? – Sayyaf Oct 31 '15 at 07:39
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

Mayuri Joshi
- 164
- 1
- 2
- 12
-
Mayuri i'll try for the one u suggested, was thinking to implement but dint knew how, Tq – Sayyaf Oct 31 '15 at 07:30