I've tried to load an image from URLs but it's not working. Two different errors are happening. When I put an URL that starts with www the app doesn't close but neither load the image, only shows a white space. But when the URL start with http:// the app closes.
The code I'm using to load the image is the next:
public class ThreadAdapter extends BaseAdapter {
ArrayList<Threadx> data;
LayoutInflater inflater;
public ThreadAdapter(Context context, ArrayList<Threadx> data) {
super();
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView tvSubject;
TextView tvContent;
ImageView imagen;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row_thread, null);
viewHolder = new ViewHolder();
viewHolder.tvSubject = (TextView) convertView
.findViewById(R.id.tvSubject);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tvContent);
viewHolder.imagen = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String subject = data.get(position).getSubject();
String content = data.get(position).getContent();
String imagen = data.get(position).getImagen();
viewHolder.tvSubject.setText(subject);
viewHolder.tvContent.setText(content);
Bitmap bitmap = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL("www.matrallune.com/images/imagen_corporativa.jpg");
//bitmap = BitmapFactory.decodeStream((InputStream)new URL("www.matrallune.com/images/imagen_corporativa.jpg").getContent());
//bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
viewHolder.imagen.setImageBitmap(bitmap);
return convertView;
}
}
I tried different methods as you can see in the code but all of them have the same issues. My intention is to catch the URL from the variable "imagen".
use this question >> Loading Bitmap to ImageView from URL in android
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private ImageView imageView;
private Bitmap image;
public DownloadImageTask(ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
try {
InputStream in = new java.net.URL(urldisplay).openStream();
image = BitmapFactory.decodeStream(in);
} catch (Exception e) {
image = null;
}
return image;
}
@SuppressLint("NewApi")
protected void onPostExecute(Bitmap result) {
if (result != null) {
imageView.setImageBitmap(result);
}
}
}
Now call in your code:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
conv`enter code here`ertView = inflater.inflate(R.layout.list_row_thread, null);
viewHolder = new ViewHolder();
viewHolder.tvSubject = (TextView) convertView
.findViewById(R.id.tvSubject);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tvContent);
viewHolder.imagen = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String subject = data.get(position).getSubject();
String content = data.get(position).getContent();
String imagen = data.get(position).getImagen();
viewHolder.tvSubject.setText(subject);
viewHolder.tvContent.setText(content);
new DownloadImageTask(viewHolder.imagen).execute("www.matrallune.com/images/imagen_corporativa.jpg");
return convertView;
}
I am guessing that you are missing urlConnection.connect();
Change your code to:
try {
URL url = new URL("www.matrallune.com/images/imagen_corporativa.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
Not sure though.
and as suggested in comments, it is wise to use library like Picasso. It is very easy. just integrate and change code to:
Picasso.with(context).load(url).into(imgView);
update: try including
connection.setDoInput(true);
setUseCaches(false)
.
Loading Image through Picasso is just like saying 123, You are done
Picasso
.with(context) //Context
.load("http://i.imgur.com/DvpvklR.png") //Can be Url,File..
.into(imageView)//The ImageView;
This is just the normal loading,you could find more options in their Documentation
According to your code
Picasso
.with(context) //Context
.load("http://matrallune.com/images/imagen_corporativa.jpg") //Can be Url,File..
.into(viewHolder.imagen)//The ImageView;
you just wanted to replace the imageloading codes with the above code.
Your Final Adapter
public class ThreadAdapter extends BaseAdapter {
ArrayList<Threadx> data;
LayoutInflater inflater;
private Context mContext;
public ThreadAdapter(Context mContext, ArrayList<Threadx> data) {
super();
inflater = LayoutInflater.from(context);
this.data = data;
this.mContext = mContext;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private static class ViewHolder {
TextView tvSubject;
TextView tvContent;
ImageView imagen;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row_thread, null);
viewHolder = new ViewHolder();
viewHolder.tvSubject = (TextView) convertView
.findViewById(R.id.tvSubject);
viewHolder.tvContent = (TextView) convertView
.findViewById(R.id.tvContent);
viewHolder.imagen = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String subject = data.get(position).getSubject();
String content = data.get(position).getContent();
String imagen = data.get(position).getImagen();
viewHolder.tvSubject.setText(subject);
viewHolder.tvContent.setText(content);
//Loading image
Picasso
.with(mContext) //Context
.load("http://matrallune.com/images/imagen_corporativa.jpg") //Can be Url,File..
.into(viewHolder.imagen)//The ImageView;
return convertView;
}
}
If you are initializing the adapter from a Fragment, you should pass the context as getActivity() or you could also initialize it in the onAttach()
of the fragment.
If you are initializing the Adapter from an Activity. you just wanted to pass this
.
Initializing
from Activity
ThreadAdapter tAdapter = new ThreadAdapter(this,data);
from Fragement
ThreadAdapter tAdapter = new ThreadAdapter(getActivity(),data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With