0

I'm making an android studio application which in one of its tabs has a rss feed and now with the addition of the image and bitmap code, whit it I try to put my image to imageview, to my adapter whenever I run my code it crashes:

    android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
        at java.net.InetAddress.getAllByName(InetAddress.java:215)
        at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
        at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
        at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
        at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
        at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
        at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
        at com.example.pauly.myapplication.LAdapter.ImgBM(LAdapter.java:54)
        at com.example.pauly.myapplication.LAdapter.getView(LAdapter.java:41)
        at android.widget.AbsListView.obtainView(AbsListView.java:2347)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillFromTop(ListView.java:759)..

Partial Ladapter.class:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Info info = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.basic_list_item, parent, false);
        }
        // Lookup view for data population
        TextView title = (TextView) convertView.findViewById(R.id.Tit);
        ImageView image = (ImageView) convertView.findViewById(R.id.img);
        //TextView description = (TextView) convertView.findViewById(R.id.Desc);
        title.setText(info.Title);
  #l.41  image.setImageBitmap(ImgBM(info.Image));
        //description.setText(info.Description);


        // Return the completed view to render on screen
        return convertView;
    }

    private Bitmap ImgBM(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
  #l.54     conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("Bitm", "Error getting bitmap", e);
        }
        return bm;
    }
}
Paul S
  • 37
  • 8
  • you should do netwrok related operation on Background threads like AsyncTask – Meenal Jun 08 '15 at 10:33
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – ben75 Jun 08 '15 at 10:50

1 Answers1

1

The issue is .NetworkOnMainThreadException Which means you are calling network on the UI. Try to use AsynkTask for downloading image.

Move this ImgBM(url) method to AsynkTask.

Amsheer
  • 7,046
  • 8
  • 47
  • 81