显示.JPG在ImageView的来自网络 [英] Displaying .jpg in an ImageView from the web

查看:90
本文介绍了显示.JPG在ImageView的来自网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我在做什么错在这里 - 它似乎出头不妥取()作为应用程序没有强制关闭时,我评论说出来节(以及任何需要它的路线返回值)。

可以使用第二双眼睛,如果你都可以这样的帮助:)!这是所有在我的主要活动的类。 image.xml(R.layout.image)是刚刚命名为imageView1的ImageView的

 公共无效toastGallery(视图V)
{
    的setContentView(R.layout.image);
    字符串URI =HTTP://www.$p$plovac.com/vladimir/wp-content/uploads/2008/03/example.jpg
    尝试
    {
        上下文的背景下= v.getContext();
        InputStream为=(InputStream的)获取(URI);
        可绘制D = Drawable.createFromStream(是,SRC);
        ImageView的imgView =新ImageView的(上下文);
        imgView =(ImageView的)findViewById(R.id.imageView1);
        imgView.setImageDrawable(四);
    }赶上(MalformedURLException的E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }}
公共对象获取(字符串地址)抛出MalformedURLException的,IOException异常{
        网址URL =新的URL(地址);
        对象内容= url.getContent();
        返回的内容;
}

logcat的信息 - 我猜这就是堆栈跟踪输出 - 请纠正我,如果不是!

 十一月一日至2日:59:09.248:D / gralloc_goldfish(971):没有模拟器检测GPU的仿真。
11月1日至2日:59:10.088:D / AndroidRuntime(971):关闭VM
11月1日至2日:59:10.088:W / dalvikvm(971):主题ID = 1:螺纹未捕获的异常(组= 0x409c01f8)退出
11月1日至2日:59:10.128:E / AndroidRuntime(971):致命异常:主要
11月1日至2日:59:10.128:E / AndroidRuntime(971):java.lang.IllegalStateException:无法执行活动的方法
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.view.View $ 1.onClick(View.java:3044)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.view.View.performClick(View.java:3511)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.view.View $ PerformClick.run(View.java:14105)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.os.Handler.handleCallback(Handler.java:605)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.os.Handler.dispatchMessage(Handler.java:92)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.os.Looper.loop(Looper.java:137)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.app.ActivityThread.main(ActivityThread.java:4424)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.lang.reflect.Method.invokeNative(本机方法)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.lang.reflect.Method.invoke(Method.java:511)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在dalvik.system.NativeStart.main(本机方法)
11月1日至2日:59:10.128:E / AndroidRuntime(971):由:java.lang.reflect.InvocationTargetException
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.lang.reflect.Method.invokeNative(本机方法)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.lang.reflect.Method.invoke(Method.java:511)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.view.View $ 1.onClick(View.java:3039)
11月1日至2日:59:10.128:E / AndroidRuntime(971):11 ...更多
11月1日至2日:59:10.128:E / AndroidRuntime(971):由:android.os.NetworkOnMainThreadException
11月1日至2日:59:10.128:E / AndroidRuntime(971):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.net.InetAddress.getAllByName(InetAddress.java:220)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpConnection<&初始化GT;(HttpConnection.java:71)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpConnection<&初始化GT;(HttpConnection.java:50)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:351)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.net.URLConnection.getContent(URLConnection.java:194)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.net.URL.getContent(URL.java:447)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在com.tjbiddle.puppywood.PuppyWood.fetch(PuppyWood.java:58)
11月1日至2日:59:10.128:E / AndroidRuntime(971):在com.tjbiddle.puppywood.PuppyWood.toastGallery(PuppyWood.java:44)
11月1日至2日:59:10.128:E / AndroidRuntime(971):14 ...更多


解决方案

蜂窝或更高版本杀死网络IO在UI线程中的所有进程。这可以解释为什么你看不到的问题,当你不取()。姜饼也有一个所谓的严格模式,它可以杀死这种网络IO或至少其标记在日志中。

如果不是这种情况,那么请张贴的堆栈跟踪,所以我们可以进一步帮助。

您的堆栈跟踪信息证实了我猜到了:

 十一月一日至2日:59:10.128:E / AndroidRuntime(971):android.os.NetworkOnMainThreadException:产生的原因
11月1日至2日:59:10.128:E / AndroidRuntime(971):在java.net.URL.getContent(URL.java:447)
[...]
11月1日至2日:59:10.128:E / AndroidRuntime(971):在com.tjbiddle.puppywood.PuppyWood.fetch(PuppyWood.java:58)

所以基本上这意味着你需要做网络IO在后台线程。实现这一目标的一种方式是例如使用的AsyncTask 作为例如在<一个href=\"https://github.com/pilhuhn/ZwitscherA/blob/master/src/de/bsd/zwitscher/OneTweetActivity.java#L803\"相对=nofollow>这个例子code 。 上preExecute onPostExecute 在UI线程同时运行 doInBackground 在自己的线程中运行。

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

解决方案

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.

这篇关于显示.JPG在ImageView的来自网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆