2013-03-05 41 views
0

我使用image-URL来表示imageView中的图像,但我的android应用程序抛出以下异常。 我正在使用异步任务来获取图像URL并将其放入onview()方法的imageview中。图像查看图像imageurl给予例外

mycode的

protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      ImageView i=(ImageView)findViewById(R.id.imageView1); 
      Bitmap bitmap; 
      Drawable d; 
      try { 
       bitmap = BitmapFactory.decodeStream((InputStream)new URL(logo.get(0)).getContent()); 
       i.setImageBitmap(bitmap); 


      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

logcat的exeception

03-05 06:34:25.148: E/AndroidRuntime(1653): FATAL EXCEPTION: main 
03-05 06:34:25.148: E/AndroidRuntime(1653): android.os.NetworkOnMainThreadException 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.net.InetAddress.getAllByName(InetAddress.java:214) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.net.URLConnection.getContent(URLConnection.java:190) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.net.URL.getContent(URL.java:447) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at com.example.slideoutmenu.LayerStack$logo.onPostExecute(LayerStack.java:249) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at com.example.slideoutmenu.LayerStack$logo.onPostExecute(LayerStack.java:1) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.os.AsyncTask.finish(AsyncTask.java:631) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.os.AsyncTask.access$600(AsyncTask.java:177) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.os.Handler.dispatchMessage(Handler.java:99) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.os.Looper.loop(Looper.java:137) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at java.lang.reflect.Method.invoke(Method.java:511) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-05 06:34:25.148: E/AndroidRuntime(1653):  at dalvik.system.NativeStart.main(Native Method) 

我怎么能显示从IMAGEURL图像。

回答

3

方法在UI线程上运行。您应该将以下行移至doInBackground()方法。

bitmap = BitmapFactory.decodeStream((InputStream)new URL(logo.get(0)).getContent());

+0

感谢快速反应 – 2013-03-05 07:24:03

+0

我设置图像视图的宽度匹配父,但图像不是在你的布局XML的imageview的标签匹配父 – 2013-03-05 07:25:50

+0

,补充一点:'机器人:scaleType =“fitXY”' – 2013-03-05 07:27:53

0

NetworkOnMainThreadException。

您正在对主线程上运行的此方法onPostExecute()进行网络操作。阅读here

在此方法中执行此网络操作doInBackground()

+1

感谢快速反应 – 2013-03-05 07:05:42

1

尝试下面的代码是有效地使用网络的任务。

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { 
    private final WeakReference<ImageView> imageViewReference; 
    private String data; 

    public BitmapWorkerTask(ImageView imageView) { 
     // Use a WeakReference to ensure the ImageView can be garbage 
     // collected 
     imageViewReference = new WeakReference<ImageView>(imageView); 
    } 

    // Decode image in background. 
    @Override 
    protected Bitmap doInBackground(String... params) { 
     data = params[0]; 
     try { 
      return BitmapFactory.decodeStream((InputStream) new URL(data) 
        .getContent()); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    // Once complete, see if ImageView is still around and set bitmap. 
    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     if (imageViewReference != null && bitmap != null) { 
      final ImageView imageView = imageViewReference.get(); 
      if (imageView != null) { 
       imageView.setImageBitmap(bitmap); 
      } 
     } 
    } 
} 

要使用该类,请使用以下语句。

ImageView img = (ImageView) findViewById(R.id.imageView1); 
BitmapWorkerTask task = new BitmapWorkerTask(img); 
task.execute("your image url"); 
+1

感谢您的快速响应 – 2013-03-05 07:23:12