I have a problem with inserting image from URL. When I try to decode Stream from BitmapFactory, application crashed
onClick method
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle extras = getIntent().getExtras();
String temp = (String) extras.get("temp");
String ikona = (String) extras.get("ikony");
URLs = "http://openweathermap.org/img/w/"+ikona+".png";
Log.d("asd",URLs);
try {
URL url = new URL(URLs);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); // here is the problem
imageView1.setImageBitmap(bmp);
} catch (MalformedURLException e) {
Log.d("asd","malformedURLException");
e.printStackTrace();
} catch (IOException e) {
Log.d("asd","IOException");
e.printStackTrace();
}
}
});
Problem is in Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream())
04-18 21:38:55.049: E/AndroidRuntime(26374): FATAL EXCEPTION: main
04-18 21:38:55.049: E/AndroidRuntime(26374): android.os.NetworkOnMainThreadException
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
04-18 21:38:55.049: E/AndroidRuntime(26374): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-18 21:38:55.049: E/AndroidRuntime(26374): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-18 21:38:55.049: E/AndroidRuntime(26374): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
04-18 21:38:55.049: E/AndroidRuntime(26374): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
04-18 21:38:55.049: E/AndroidRuntime(26374): at pl.pawelfrydrych.realweather.Szczegoly$1.onClick(Szczegoly.java:55)
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.view.View.performClick(View.java:4475)
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.view.View$PerformClick.run(View.java:18786)
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.os.Handler.handleCallback(Handler.java:730)
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.os.Looper.loop(Looper.java:137)
04-18 21:38:55.049: E/AndroidRuntime(26374): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-18 21:38:55.049: E/AndroidRuntime(26374): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 21:38:55.049: E/AndroidRuntime(26374): at java.lang.reflect.Method.invoke(Method.java:525)
04-18 21:38:55.049: E/AndroidRuntime(26374): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-18 21:38:55.049: E/AndroidRuntime(26374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-18 21:38:55.049: E/AndroidRuntime(26374): at dalvik.system.NativeStart.main(Native Method)
You can not make a network call on the main thread. Do the bitmap decoding in a separate thread or in an AsyncTask. Also make sure that you have permission for internet.
You can't get a Bitmap image from url directly run it on thread.
Bundle extras = getIntent().getExtras();
String temp = (String) extras.get("temp");
String ikona = (String) extras.get("ikony");
URLs = "http://openweathermap.org/img/w/"+ikona+".png";
Log.d("asd",URLs);
Thread thread = new Thread(new Runnable() {
public void run() {
try {
URL url = new URL(URLs);
Bitmap bmp= BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
imageView1.setImageBitmap(bmp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.