图片不会从URL android加载

时间:2013-08-11 19:08:42

标签: android http imageview bitmapimage

继续

08-11 20:23:54.674: ERROR/AndroidRuntime(10035): FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
    at com.example.eventcloud.EventObject.loadBitmap(EventObject.java:84)
    at com.example.eventcloud.MyCustomBaseAdapter.getView(MyCustomBaseAdapter.java:53)
    at android.widget.AbsListView.obtainView(AbsListView.java:2177)
    at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
    at android.widget.ListView.onMeasure(ListView.java:1159)
    at android.view.View.measure(View.java:15848)
    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15848)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1447)
    at android.view.View.measure(View.java:15848)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

这是处理位图的函数,它崩溃的行标记为

public Bitmap loadBitmap() {

    try
    {
        URL url = new URL(pictureURL);   
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();   //LINE
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);

        return myBitmap;
    }
    catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

PictureURL = http://event-cloud.co.uk/events/uploads/images/eventthumb.png

2 个答案:

答案 0 :(得分:0)

它需要http://或您想要的任何其他协议。 pictureURL的价值是什么?

答案 1 :(得分:0)

你得到NetworkOnMainThreadException。这意味着,您正在UI线程上执行网络操作。使用AsyncTask