This question has already been answered:

I want to display an image from a site in ImageView . I found such code in the video tutorial, but it does not work - the compiler issues an error warning on the line connction.connect() and then in the getBitmapfromURL() method. Please tell me where I made a mistake?

 package com.csgobets.bets; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageButton; import android.widget.ImageView; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.logging.Level; public class MainActivity extends ActionBarActivity { Bitmap image; ImageButton iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageButton) findViewById(R.id.imageButton); Bitmap bitmap = getBitmapFromURL(); iv.setImageBitmap(bitmap); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } public Bitmap getBitmapFromURL(){ try { URL url = new URL("http://daypic.ru/wp-content/uploads/2013/08/1031-900x562.jpg"); Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream()); return image; } catch (Exception e){ e.printStackTrace(); return null; } } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 

log:

 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• android.os.NetworkOnMainThreadException 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1155) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at java.net.InetAddress.getAllByName(InetAddress.java:215) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:236) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:190) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.csgobets.bets.MainActivity.getBitmapFromURL(MainActivity.java:43) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.csgobets.bets.MainActivity.onCreate(MainActivity.java:29) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.Activity.performCreate(Activity.java:5958) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.ActivityThread.access$800(ActivityThread.java:144) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.os.Handler.dispatchMessage(Handler.java:102) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.os.Looper.loop(Looper.java:155) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at android.app.ActivityThread.main(ActivityThread.java:5696) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at java.lang.reflect.Method.invoke(Native Method) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at java.lang.reflect.Method.invoke(Method.java:372) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 05-09 16:49:49.548 5307-5307/com.csgobets.bets W/System.errīš• at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

Reported as a duplicate by members of Pavel Mayorov , ߊߚߤߘ , Regent , sanmai , Community Spirit â™Ļ 13 Oct '17 at 7:59 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Give the error message right in the question, along with the command that was used for the compilation. And use this message to make the question title more informative. - jfs
  • one
    This code will only work on older versions of the adroid. On the new - no (4 android and older - for sure). In new versions you need via asyncTask or thread. In the main thread (where the UI works) it is impossible to climb into the network! - KoVadim
  • Use the Picasso or Universal Image Loader libraries. do not write your "bike" - DeKaNszn

1 answer 1

NetworkOnMainThreadException is your answer. You can not work with the network in the main stream.

Here is the solution from here

 class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String... urls) { try { URL url= new URL(urls[0]); SAXParserFactory factory =SAXParserFactory.newInstance(); SAXParser parser=factory.newSAXParser(); XMLReader xmlreader=parser.getXMLReader(); RssHandler theRSSHandler=new RssHandler(); xmlreader.setContentHandler(theRSSHandler); InputSource is=new InputSource(url.openStream()); xmlreader.parse(is); return theRSSHandler.getFeed(); } catch (Exception e) { this.exception = e; return null; } } protected void onPostExecute(RSSFeed feed) { // TODO: check this.exception // TODO: do something with the feed } }