从网址加载图片

时间:2014-12-26 12:57:02

标签: java android

我想从网址加载图片,我正在使用此代码:

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());

2 个答案:

答案 0 :(得分:2)

  

有人知道为什么吗?

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

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

  

还有其他代码兼容吗?

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

答案 1 :(得分:0)

private void callWeb(URL myurl) {
    HttpGet httpRequest = null;
    httpRequest = new HttpGet(myurl.toURI());

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream inputStream = null;
        try {
            inputStream = entity.getContent();
            BitmapFactory.Options options = new BitmapFactory.Options();
            // options.inSampleSize = 2;
            final Bitmap bitmap = BitmapFactory.decodeStream(inputStream,
                    null, options);
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            entity.consumeContent();
        }
    }
}

检查是否有帮助?