从远程服务器获取图像是一个好的简单的方法吗?当我试着在 android AVD 实现这项功能时,在屏幕上和 log_cat上什么也没显示,没有错误也没有崩溃。如何解决这个问题呢?public class MainActivity extends Activity {
Bitmap bitmap = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
bitmap = getBitmap("https://twimg0-a.akamaihd.net/profile_images/2275552571/image_normal.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}catch(Exception e){
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
}catch(Exception ex) {return null;}
}
}
3 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
private Drawable LoadImageFromWebOperations(String url) { try { InputStream is = (InputStream) new URL(url).getContent(); Drawable d = Drawable.createFromStream(is, "image.png"); return d; } catch (Exception e) { System.out.println("Exc=" + e); return null; } }
添加回答
举报
0/150
提交
取消