在Web上的ImageView中显示.jpg

时间:2012-01-02 16:41:50

标签: java android image url

我不确定我在这里做错了什么 - 似乎fetch()的某些东西是错误的,因为当我评论那个部分时,应用程序没有强制关闭(当然,任何需要它的东西都是返回值)。

如果你们都这么有帮助,可以使用第二双眼睛:)!这都在我的主要活动课程中。 image.xml(R.layout.image)只有一个名为“imageView1”的ImageView

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信息 - 我猜这是StackTrace输出的地方 - 如果没有请纠正我!

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 个答案:

答案 0 :(得分:0)

Honeycomb或更高版本正在杀死UI线程中网络IO的所有进程。这可以解释为什么在没有fetch()时没有看到问题。 Gingerbread也有一个所谓的严格模式,它可以杀死这样的网络IO或者至少在日志中标记它。

如果不是这种情况,请发布堆栈跟踪,以便我们进一步提供帮助。

你的堆栈跟踪证实了我的猜测:

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)

所以基本上这意味着你需要在后台线程中进行网络IO。实现此目的的一种方法是例如使用AsyncTask作为例如在this example codeonPreExecuteonPostExecute在UI线程中运行,doInBackground在自己的线程中运行。