Android Exibindo uma imagem da web em um ImageView

ImageView web
Exibindo uma imagem em um ImageView

Crie uma ImageView em seu xml:

    <ImageView 
    android:id="@+id/imagem"
    android:layout_width="250px"
    android:layout_height="250px"
    android:layout_gravity="center_horizontal"
    >
    </ImageView>

Bom agora vamos pegar a imagem de uma Url com os seguintes métodos:

    public Drawable getImagem() throws Exception {
        url = new URL("http://minhaimagem.com/img");

        InputStream is = (InputStream) getObjeto(url);
        Drawable d = Drawable.createFromStream(is, "src");

        return d;
    }

    private Object getObjeto(URL url) throws MalformedURLException, IOException {
        Object content = url.getContent();
        return content;
    }

Após ter criado o método basta adicionar a imagem em seu listView:

	Drawable image = getImage();
	ImageView imgView = (ImageView)findViewById(R.id.imagem);
	imgView.setImageDrawable(image);

Lembre-se de incluir a permissão de acesso a internet em seu Manifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>

Obrigado.
Guilherme Biff Zarelli

Help DEV – Analista desenvolvedor Java / Android
https://helpdev.com.br/zarelli

Android Exibindo uma imagem da web em um ImageView

5 pensou em “Android Exibindo uma imagem da web em um ImageView

  1. Como acredito que muitos precisarao fazer uma tela parecida decidi compartilhar com voces a solucao…Segue abaixo o codigo main.xml ..?xml version 1.0 encoding utf-8 ?.RelativeLayout xmlns android http schemas.android.com apk res android .android orientation vertical android gravity center horizontal .android layout height fill parent android layout width fill parent ..LinearLayout android layout width fill parent . TextView android textColor FFFFFF android layout height wrap content . android layout alignParentBottom true android layout width fill parent .

Deixe uma resposta para business daily Cancelar resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

Esse site utiliza o Akismet para reduzir spam. Aprenda como seus dados de comentários são processados.

Rolar para o topo