I am trying to display images in a listview from links in a json file, along with textviews,and then display them in a rounded shape. however the textviews show but not the images. This is a code snippet for what I tried for the images;
public class ChatsArrayAdapter extends ArrayAdapter<ChatData>
{
ImageLoader imageLoader;
public ChatsArrayAdapter(Context context, List<ChatData> objects)
{
super(context, 0, objects);
//imageLoader=new ImageLoader(imageviewfromchat, avatar_url);
}
public View getView(int position, View convertView, ViewGroup parent)
{
ChatCell chatCell = new ChatCell();
ImageLoader imageLoader=new ImageLoader(getContext());
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.cell_chat, parent, false);
chatCell.imageviewfromchat=(ImageView)convertView.findViewById(R.id.imageviewfromchat);
ChatData chatData = getItem(position);
imageLoader.DisplayImage(chatData.avatarURL,chatCell.imageviewfromchat);
//imageLoader.getBitmap(chatData.avatarURL,chatCell.imageviewfromchat);
return convertView;
}
private static class ChatCell
{
ImageView imageviewfromchat;
}
private class ImageLoader {
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
ExecutorService executorService;
// Handler to display images in UI thread
public ImageLoader(Context context) {
executorService = Executors.newFixedThreadPool(5);
}
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
getBitmap(url);
//getRoundedShape(bitmap)
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
}
private Bitmap getBitmap(String url) {
// Download Images from the Internet
Bitmap bitmap = null;
try {
bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = conn.getOutputStream();
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
return null;
}
return bitmap;
}
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 50;
int targetHeight = 50;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), null);
return targetBitmap;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
}
private static class Utils {
public static void CopyStream(InputStream is, OutputStream os)
{
final int buffer_size=1024;
try
{
byte[] bytes=new byte[buffer_size];
for(;;)
{
int count=is.read(bytes, 0, buffer_size);
if(count==-1)
break;
os.write(bytes, 0, count);
}
}
catch(Exception ex){}
}
}
}
i believe my code is correct but I could be wrong. Could anyone please help? IT IS NOT A NETWORK ONTHREAD EXCEPTION DUPLICATE I AM JUST TRYING TO DISPLAY IMAGES IN MY LISTVIEW SELVIN
Use Picasso for image loading
Picasso.with(context).load(imgUrl)
.placeholder(R.drawable.img_placeholder)
.error(R.drawable.error_img)
.into(holder.desire_img);
for reference you can use link here