Harshil Pansare
Harshil Pansare

Reputation: 1147

Getting Images from url in ListView

I am trying to get images from url and set it in a listview through my listviewadapter class. I am getting NULLPOINTEREXCEPTION at openConnection() in getBitmapFromUrl method at the end. Here is my code:

ListViewAdapter.java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

    public String msgType[] = new String[8];
    public String msgData[] = new String[8];
    public String msgTimeStamp[] = new String[8];
    Bitmap[] myBitmap = new Bitmap[8];
    public Activity context;
    int positionX;
    URL urlX;
    public LayoutInflater inflater;

    public ListViewAdapter(Activity context, String[] type, String[] data,
            String[] timestamp) {
        super();

        this.context = context;
        this.msgType = type;
        this.msgData = data;
        this.msgTimeStamp = timestamp;

        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return msgData.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public static class ViewHolder {
        ImageView imgView;
        TextView txtType;
        TextView txtData;
        TextView txtTimestamp;
        Bitmap myBitmap;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);

            holder.imgView = (ImageView) convertView.findViewById(R.id.image);
            holder.txtType = (TextView) convertView.findViewById(R.id.type);
            holder.txtData = (TextView) convertView.findViewById(R.id.data);
            holder.txtTimestamp = (TextView) convertView
                    .findViewById(R.id.timestamp);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        // holder.imgView.setImageBitmap(myBitmap[position]);
        holder.txtType.setText(msgType[position]);
        holder.txtTimestamp.setText(msgTimeStamp[position]);
        if (msgType[position].equals("0"))
            holder.txtData.setText(msgData[position]);
        else {
            holder.txtData.setText("");
            URL url = null;
            try {
                url = new URL(msgData[position]);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
urlX=url;
        positionX=position;
        try {
        Thread t = new Thread(new Runnable() { 
            public void run(){         
                myBitmap[positionX] = getBitmapFromUrl(urlX);
            } 
        });
        t.start();

            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            holder.imgView.setImageBitmap(myBitmap[position]);
        }

        return convertView;
    }





    public Bitmap getBitmapFromUrl(URL url1) {
        Bitmap Bitmap = null;
        try {
            HttpURLConnection connection = (HttpURLConnection) url1
                    .openConnection();
            connection.setDoInput(true);

            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap = BitmapFactory.decodeStream(input);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return Bitmap;
    }

}

EDIT:

Thread t = new Thread(new Runnable() { 
            public void run(){         
                myBitmap[positionX] = getBitmapFromUrl(urlX);
            } 
        });
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

LOGCAT(after edit):

04-03 12:01:26.860: E/AndroidRuntime(11944): FATAL EXCEPTION: Thread-1155
04-03 12:01:26.860: E/AndroidRuntime(11944): java.lang.NullPointerException
04-03 12:01:26.860: E/AndroidRuntime(11944):    at com.appquest.flatcassign.ListViewAdapter.getBitmapFromUrl(ListViewAdapter.java:127)
04-03 12:01:26.860: E/AndroidRuntime(11944):    at com.appquest.flatcassign.ListViewAdapter$1.run(ListViewAdapter.java:107)
04-03 12:01:26.860: E/AndroidRuntime(11944):    at java.lang.Thread.run(Thread.java:856)

Upvotes: 0

Views: 61

Answers (1)

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

Hi I update your base adpter class see blow with implement of the picasso library, you just have to copy and replace the code, sure help you. do not forget to add the jar file.

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {

    public String msgType[] = new String[8];
    public String msgData[] = new String[8];
    public String msgTimeStamp[] = new String[8];
    public Activity context;
    public LayoutInflater inflater;

    public ListViewAdapter(Activity context, String[] type, String[] data,
            String[] timestamp) {
        super();

        this.context = context;
        this.msgType = type;
        this.msgData = data;
        this.msgTimeStamp = timestamp;

        this.inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return msgData.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        ImageView imgView;
        TextView txtType;
        TextView txtData;
        TextView txtTimestamp;
        Bitmap myBitmap;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null);

            holder.imgView = (ImageView) convertView.findViewById(R.id.image);
            holder.txtType = (TextView) convertView.findViewById(R.id.type);
            holder.txtData = (TextView) convertView.findViewById(R.id.data);
            holder.txtTimestamp = (TextView) convertView
                    .findViewById(R.id.timestamp);

            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        // holder.imgView.setImageBitmap(myBitmap[position]);
        holder.txtType.setText(msgType[position]);
        holder.txtTimestamp.setText(msgTimeStamp[position]);
        if (msgType[position].equals("0"))
            holder.txtData.setText(msgData[position]);
        else {
            holder.txtData.setText("");
            Picasso.with(context).load(msgData[position]).into(holder.imgView);
        }
        return convertView;
    }
}

Thank you.

Upvotes: 1

Related Questions