Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't load an image from URL

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".

like image 928
Manel Mendez Avatar asked May 13 '15 16:05

Manel Mendez


3 Answers

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;
}
like image 145
Maycon Cardoso Avatar answered Nov 10 '22 22:11

Maycon Cardoso


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).

like image 39
Krupal Shah Avatar answered Nov 11 '22 00:11

Krupal Shah


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);
like image 1
theapache64 Avatar answered Nov 10 '22 22:11

theapache64