试了
image = (ImageView) findViewById(R.id.img);
image.setImageURI(Uri.parse("http://XXXX/Test/index/4.png")); 方法,不知道是什么原因,图片总是显示不出来
下面方法可以将服务器端的图片显示到ImageView上
try { image.setImageBitmap(GetServerBitmap(new URL( "http://XXXXX/Test/index/2.jpg"))); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
GetServerBitmap方法
public static Bitmap GetServerBitmap(URL url) throws IOException { URLConnection conn = url.openConnection(); try { conn.connect(); InputStream is = conn.getInputStream(); Bitmap bmp = BitmapFactory.decodeStream(is); is.close(); return bmp; } catch (Exception e) { // TODO: handle exception return null; } }
同时记得在AndroidManifest.xml中申明访问权限,这步很重要,最初就是忘记了在里面申明访问为网络资源的权限,调试了很久
<uses-permission android:name="android.permission.INTERNET"/>