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)