Displaying .jpg in an ImageView from the web

2019-07-09 22:13发布

I'm not sure what I'm doing wrong here - it seems that somethings is wrong with fetch() as the application didn't force close when I commented that section out (And anything that needed it's return value of course).

Could use a second pair of eyes if you all could be so helpful :)! This is all within my main Activity's class. image.xml (R.layout.image) is has just an ImageView named "imageView1"

public void toastGallery(View v)
{
    setContentView(R.layout.image);
    String uri = "http://www.prelovac.com/vladimir/wp-content/uploads/2008/03/example.jpg";
    try
    {
        Context context = v.getContext();
        InputStream is = (InputStream) fetch(uri);
        Drawable d = Drawable.createFromStream(is, "src");
        ImageView imgView = new ImageView(context);
        imgView = (ImageView)findViewById(R.id.imageView1);
        imgView.setImageDrawable(d);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;     
}

Logcat info - I'm guessing this is where the StackTrace outputs - please correct me if not!

01-02 11:59:09.248: D/gralloc_goldfish(971): Emulator without GPU emulation detected.
01-02 11:59:10.088: D/AndroidRuntime(971): Shutting down VM
01-02 11:59:10.088: W/dalvikvm(971): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-02 11:59:10.128: E/AndroidRuntime(971): FATAL EXCEPTION: main
01-02 11:59:10.128: E/AndroidRuntime(971): java.lang.IllegalStateException: Could not execute method of the activity
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.view.View$1.onClick(View.java:3044)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.view.View.performClick(View.java:3511)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.view.View$PerformClick.run(View.java:14105)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.os.Handler.handleCallback(Handler.java:605)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.os.Looper.loop(Looper.java:137)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:511)
01-02 11:59:10.128: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-02 11:59:10.128: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-02 11:59:10.128: E/AndroidRuntime(971):  at dalvik.system.NativeStart.main(Native Method)
01-02 11:59:10.128: E/AndroidRuntime(971): Caused by: java.lang.reflect.InvocationTargetException
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:511)
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.view.View$1.onClick(View.java:3039)
01-02 11:59:10.128: E/AndroidRuntime(971):  ... 11 more
01-02 11:59:10.128: E/AndroidRuntime(971): Caused by: android.os.NetworkOnMainThreadException
01-02 11:59:10.128: E/AndroidRuntime(971):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-02 11:59:10.128: E/AndroidRuntime(971):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.net.URLConnection.getContent(URLConnection.java:194)
01-02 11:59:10.128: E/AndroidRuntime(971):  at java.net.URL.getContent(URL.java:447)
01-02 11:59:10.128: E/AndroidRuntime(971):  at com.tjbiddle.puppywood.PuppyWood.fetch(PuppyWood.java:58)
01-02 11:59:10.128: E/AndroidRuntime(971):  at com.tjbiddle.puppywood.PuppyWood.toastGallery(PuppyWood.java:44)
01-02 11:59:10.128: E/AndroidRuntime(971):  ... 14 more

1条回答
祖国的老花朵
2楼-- · 2019-07-09 22:31

Honeycomb or later is killing all processes that to network IO in the UI thread. This could explain why you don't see the issue when you don't fetch(). Gingerbread also has a so called strict mode, which can kill such network IO or at least flag it in the logs.

If this is not the case, then please post the stack trace so we can help further.

Your stacktrace confirms what I guessed:

01-02 11:59:10.128: E/AndroidRuntime(971): Caused by: android.os.NetworkOnMainThreadException
01-02 11:59:10.128: E/AndroidRuntime(971):      at java.net.URL.getContent(URL.java:447)
[...]
01-02 11:59:10.128: E/AndroidRuntime(971):      at com.tjbiddle.puppywood.PuppyWood.fetch(PuppyWood.java:58)

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 AsyncTask as e.g. in this example code. onPreExecuteand onPostExecute run in the UI thread while doInBackgroundruns in its own thread.

查看更多
登录 后发表回答