This question already has answers here:
How to fix 'android.os.NetworkOnMainThreadException'?
                                
                                    (58个答案)
                                
                        
                5年前关闭。
            
        

我想从网址加载图片,并且正在使用以下代码:

URL url = new URL(myurl);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);


我正在具有android 2.3的设备中运行该应用程序,并且运行正常,但在具有android 4.4的其他设备中应用程序崩溃并关闭。

有人知道为什么吗?是否有其他兼容的代码?

谢谢

这是我的日志猫:

12-26 15:17:01.993: E/AndroidRuntime(3878): FATAL EXCEPTION: main
12-26 15:17:01.993: E/AndroidRuntime(3878): Process: com.my.feed, PID: 3878
12-26 15:17:01.993: E/AndroidRuntime(3878): android.os.NetworkOnMainThreadException
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.my.feed.LazyAdapterComentarios.getView(LazyAdapterComentarios.java:85)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.AbsListView.obtainView(AbsListView.java:2255)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.ListView.onMeasure(ListView.java:1175)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.View.measure(View.java:16521)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-26 15:17:01.993: E/AndroidRuntime(3878):     at android.view.Choreographer.doCallbacks(Choreographer


崩溃的行是:

Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());

最佳答案

有人知道为什么吗?


您应该通过examining LogCat并查看与崩溃相关的Java堆栈跟踪。

在这种情况下,我希望您会发现您正在获得a NetworkOnMainThreadException


  是否有其他兼容的代码?


manymany个库,用于从网络上异步加载图像并使用它们填充ImageView。就个人而言,我倾向于Picasso

09-28 02:17