我想从一个URL获取JPEG并将其放置在我的Android应用程序的图像视图中。我正在使用将URL中的图像变成可绘制的方法。然而,图像显示不出来,图像视图仅仅是空白图像不显示在Android应用程序
这里是链接到JPEG:http://upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg
方法:
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
主要功能:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
ImageView profilePhoto = (ImageView)findViewById(R.id.personImage);
String url = "http://upload.wikimedia.org/wikipedia/commons/5/57/PT05_ubt.jpeg";
profilePhoto.setImageDrawable(LoadImageFromWebOperations(url));
}
如果您还没有,请引用日志或进行调试,以确保您没有在'LoadImageFromWebOperations'中获得异常。另外,如果你使用本地的东西(比如制作自己的drawable)来确保没有布局问题,那么确保你能够看到ImageView。 – Jakar