【发布时间】:2014-07-31 20:59:33
【问题描述】:
我正在尝试从 url 获取 jpeg 并将其放置在我的 android 应用程序的图像视图中。我正在使用一种将图像从 url 转换为 drawable 的方法。但是,图像没有显示出来,图像视图只是空白
这是 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中收到异常。此外,如果您使用本地的东西(例如,制作自己的可绘制对象),请确保您能够看到 ImageView,以确保没有一些布局问题。