Olá Vinny,
Essa exception ocorre porque você está tentando realizar uma operação de acesso à rede dentro da thread principal, que é reservada para operações de UI (user interface). Faça essa operação em uma thread separada, por exemplo usando uma AsyncTask (que executa o que estiver dentro do método doInBackground() numa thread à parte):class LoadImageTask extends AsyncTask<String, Void, Drawable> {
protected Drawable doInBackground(String... url) {
Drawable drawable = null;
try {
drawable = LoadImageFromWebOperations(url[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return drawable;
}
protected void onPostExecute(Drawable drawable) {
ImageView imgView =(ImageView)findViewById(R.id.teste);
imgView.setImageDrawable(drawable);
}
}
O que está acontecendo é que a asynctask espera o método doInBackground() executar, e quando ele termina passa a ser executado o método onPostExecute(), que volta a ter acesso à thread de UI e pode executar operações de UI, como por exemplo chamar setImageDrawable() para exibir o drawable.
Para chamar a asynctask no seu código você faz assim:
LoadImageTask task = new LoadImageTask();
task.execute("http://marciocardoso.net/img/Linux_avatar.jpg");
[]'s
On Fri, Jun 28, 2013 at 4:57 PM, Vinny Valente <irmaocaradepau@gmail.com> wrote:
Boa tarde Galera,Estou tentando exibir uma imagem da web, mas ta complicado.Todas as formas que eu li caem na mesmo esquema, justamente onde da o erro...Nessa linhaInputStream is = (InputStream) new URL(url).getContent();O getContent() sempre cai na exeption...Segue erro06-28 16:29:00.450: E/AndroidRuntime(14548): FATAL EXCEPTION: main06-28 16:29:00.450: E/AndroidRuntime(14548): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slideshow/com.example.slideshow.MainActivity}: android.os.NetworkOnMainThreadException06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.ActivityThread.access$600(ActivityThread.java:140)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.os.Handler.dispatchMessage(Handler.java:99)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.os.Looper.loop(Looper.java:137)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.ActivityThread.main(ActivityThread.java:4898)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.lang.reflect.Method.invokeNative(Native Method)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.lang.reflect.Method.invoke(Method.java:511)06-28 16:29:00.450: E/AndroidRuntime(14548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)06-28 16:29:00.450: E/AndroidRuntime(14548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)06-28 16:29:00.450: E/AndroidRuntime(14548): at dalvik.system.NativeStart.main(Native Method)06-28 16:29:00.450: E/AndroidRuntime(14548): Caused by: android.os.NetworkOnMainThreadException06-28 16:29:00.450: E/AndroidRuntime(14548): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.net.InetAddress.getAllByName(InetAddress.java:214)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)06-28 16:29:00.450: E/AndroidRuntime(14548): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.net.URLConnection.getContent(URLConnection.java:190)06-28 16:29:00.450: E/AndroidRuntime(14548): at java.net.URL.getContent(URL.java:447)06-28 16:29:00.450: E/AndroidRuntime(14548): at com.example.slideshow.MainActivity.LoadImageFromWebOperations(MainActivity.java:114)06-28 16:29:00.450: E/AndroidRuntime(14548): at com.example.slideshow.MainActivity.onCreate(MainActivity.java:38)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.Activity.performCreate(Activity.java:5206)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)06-28 16:29:00.450: E/AndroidRuntime(14548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)06-28 16:29:00.450: E/AndroidRuntime(14548): ... 11 moreCodigo:ImageView imgView =(ImageView)findViewById(R.id.teste);Drawable drawable = null;try {drawable = LoadImageFromWebOperations("http://marciocardoso.net/img/Linux_avatar.jpg");} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}imgView.setImageDrawable(drawable);--private Drawable LoadImageFromWebOperations(String url) throws MalformedURLException, IOException {InputStream is = (InputStream) new URL(url).getContent();Drawable d = Drawable.createFromStream(is, "src name");return d;}
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.






0 comentários:
Postar um comentário