不要在线显示Android图片获取URL

时间:2015-03-11 08:40:14

标签: android

我想在线显示图片(网址),但它没有显示任何内容,我也不知道为什么会这样。我也在清单中添加了INTERNET权限。

    private ImageView iv;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String address = "http://4.bp.blogspot.com/-3_EM3_dBstc/UxD7U2EEa2I/AAAAAAAACmI/M9JilH6eIV0/s100/Krita-logo.png";

        iv = (ImageView) findViewById(R.id.image);
        bitmap = bitImageRead(address);
        iv.setImageBitmap(bitmap);
    }

我哪里做错了?

1 个答案:

答案 0 :(得分:2)

试试这个..

public Bitmap bitImageRead(address){
 try {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap myBitmap = BitmapFactory.decodeStream(input);
    return myBitmap;
} catch (IOException e) {
    e.printStackTrace();
    return null;
}
}