{"id":1440,"date":"2022-08-30T15:16:33","date_gmt":"2022-08-30T15:16:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/displaying-jpg-in-an-imageview-from-the-web-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:33","modified_gmt":"2022-08-30T15:16:33","slug":"displaying-jpg-in-an-imageview-from-the-web-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/displaying-jpg-in-an-imageview-from-the-web-collection-of-common-programming-errors\/","title":{"rendered":"Displaying .jpg in an ImageView from the web-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m not sure what I&#8217;m doing wrong here &#8211; it seems that somethings is wrong with fetch() as the application didn&#8217;t force close when I commented that section out (And anything that needed it&#8217;s return value of course).<\/p>\n<p>Could use a second pair of eyes if you all could be so helpful :)! This is all within my main Activity&#8217;s class. image.xml (R.layout.image) is has just an ImageView named &#8220;imageView1&#8221;<\/p>\n<pre><code>public void toastGallery(View v)\n{\n    setContentView(R.layout.image);\n    String uri = \"http:\/\/www.prelovac.com\/vladimir\/wp-content\/uploads\/2008\/03\/example.jpg\";\n    try\n    {\n        Context context = v.getContext();\n        InputStream is = (InputStream) fetch(uri);\n        Drawable d = Drawable.createFromStream(is, \"src\");\n        ImageView imgView = new ImageView(context);\n        imgView = (ImageView)findViewById(R.id.imageView1);\n        imgView.setImageDrawable(d);\n    } catch (MalformedURLException e) {\n        e.printStackTrace();\n    } catch (IOException e) {\n        e.printStackTrace();\n    }\n\n}\npublic Object fetch(String address) throws MalformedURLException,IOException {\n        URL url = new URL(address);\n        Object content = url.getContent();\n        return content;     \n}\n<\/code><\/pre>\n<p>Logcat info &#8211; I&#8217;m guessing this is where the StackTrace outputs &#8211; please correct me if not!<\/p>\n<pre><code>01-02 11:59:09.248: D\/gralloc_goldfish(971): Emulator without GPU emulation detected.\n01-02 11:59:10.088: D\/AndroidRuntime(971): Shutting down VM\n01-02 11:59:10.088: W\/dalvikvm(971): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)\n01-02 11:59:10.128: E\/AndroidRuntime(971): FATAL EXCEPTION: main\n01-02 11:59:10.128: E\/AndroidRuntime(971): java.lang.IllegalStateException: Could not execute method of the activity\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.view.View$1.onClick(View.java:3044)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.view.View.performClick(View.java:3511)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.view.View$PerformClick.run(View.java:14105)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.os.Handler.handleCallback(Handler.java:605)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.os.Handler.dispatchMessage(Handler.java:92)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.os.Looper.loop(Looper.java:137)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.app.ActivityThread.main(ActivityThread.java:4424)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:511)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at dalvik.system.NativeStart.main(Native Method)\n01-02 11:59:10.128: E\/AndroidRuntime(971): Caused by: java.lang.reflect.InvocationTargetException\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:511)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.view.View$1.onClick(View.java:3039)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  ... 11 more\n01-02 11:59:10.128: E\/AndroidRuntime(971): Caused by: android.os.NetworkOnMainThreadException\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.net.InetAddress.getAllByName(InetAddress.java:220)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpConnection.(HttpConnection.java:71)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpConnection.(HttpConnection.java:50)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.net.URLConnection.getContent(URLConnection.java:194)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at java.net.URL.getContent(URL.java:447)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at com.tjbiddle.puppywood.PuppyWood.fetch(PuppyWood.java:58)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  at com.tjbiddle.puppywood.PuppyWood.toastGallery(PuppyWood.java:44)\n01-02 11:59:10.128: E\/AndroidRuntime(971):  ... 14 more\n<\/code><\/pre>\n<ol>\n<li>\n<p>Honeycomb or later is killing all processes that to network IO in the UI thread. This could explain why you don&#8217;t see the issue when you don&#8217;t <code>fetch()<\/code>. Gingerbread also has a so called strict mode, which can kill such network IO or at least flag it in the logs.<\/p>\n<p>If this is not the case, then please post the stack trace so we can help further.<\/p>\n<p>Your stacktrace confirms what I guessed:<\/p>\n<pre><code>01-02 11:59:10.128: E\/AndroidRuntime(971): Caused by: android.os.NetworkOnMainThreadException\n01-02 11:59:10.128: E\/AndroidRuntime(971):      at java.net.URL.getContent(URL.java:447)\n[...]\n01-02 11:59:10.128: E\/AndroidRuntime(971):      at com.tjbiddle.puppywood.PuppyWood.fetch(PuppyWood.java:58)\n<\/code><\/pre>\n<p>So basically that means that you need to do Network IO in a background thread. One way to achieve this is to e.g. use an <code>AsyncTask<\/code> as e.g. in this example code. <code>onPreExecute<\/code>and <code>onPostExecute<\/code> run in the UI thread while <code>doInBackground<\/code>runs in its own thread.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:53:18. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m not sure what I&#8217;m doing wrong here &#8211; it seems that somethings is wrong with fetch() as the application didn&#8217;t force close when I commented that section out (And anything that needed it&#8217;s return value of course). Could use a second pair of eyes if you all could be so helpful :)! This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1440","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1440"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1440\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}