试了
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"/>
本文详细介绍了在Android应用中遇到图片无法显示的问题,并提供了解决方案:使用URL加载图片到ImageView,同时强调了在AndroidManifest.xml中声明访问网络资源的权限的重要性。通过实例演示了如何使用自定义的GetServerBitmap方法从服务器获取并显示图片,以及在代码中正确调用此方法的步骤。
450

被折叠的 条评论
为什么被折叠?



