Can I put an image of the internet on an Imagebutton?

Asked

Viewed 315 times

5

It is possible to create a imageButton and within it put an image of internet. I can do that with webView, but it is possible to do with the imageButton?

2 answers

7


I think the simplest way is this:

URL imageUrl = new URL("url_da_imagem"); 
Bitmap imagem = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
meuImageButton.setImageBitmap(imagem); 

Source Soen

However the correct thing is to do this procedure in a Asynctask:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageButton imageButton;

    public DownloadImageTask(ImageButton imageButton) {
        this.imageButton = imageButton;
    }

    protected Bitmap doInBackground(String... urls) {
        URL imageUrl = null;
        try {
            imageUrl = new URL(urls[0]);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Bitmap imagem = null;
        try {
            imagem = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return imagem;
    }

    protected void onPostExecute(Bitmap result) {
        imageButton.setImageBitmap(result);
    }    
}

To place the image on your Imagebutton do:

ImageButton imageButton = (ImageButton) findViewById(R.id.doSeuImageButton);
new DownloadImageTask(imageButton).execute("url_da_imagem");  

It is necessary to add the following permission to Androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />
  • Hello, thanks for the reply, I’m beginner on android and could not understand very well how I can use, I’m studying and seeing how I can use. Thank you

  • It’s easy. Copy the class code DownloadImageTask to the Activity where you have the Imagebutton. Copy the last answer code into the onCreate of that Activity. Has to replace R.id.doSeuImageButton and url_da_imagem at the right values.

  • I got it before you talk, researched it and that’s what you said, now I’ll try to see how to resize the image to imageButton, thank you so much for the reply again.

  • You will need to add <uses-permission android:name="android.permission.INTERNET" /> at the Androidmanifest.xml

  • At stackoverflow the best way to thank those who helped you is to signal the best answer and/or vote for those who were helpful.

  • I already did that thanked. This android.permission is to increase?

  • To increase what? The image? Permission is required for the application to access the internet. Without this the code will not work.

  • It worked without using Androidmanifest. Ahhhhh but it was pq I had already used for something else I tried to do with webView.

  • It would be the size of the image to adapt to the size of the imageButton, I’m looking to see if axo how to do.

  • In the xml of ImageButton put these two attributes: android:scaleType="fitCenter" and android:adjustViewBounds="true"

Show 5 more comments

3

First you need to download this image because there is no direct way to inform a URL directly on the property src, for example.

You can have a method like this, to download this and get a Drawable:

public Drawable drawableFromUrl(String url) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    x = BitmapFactory.decodeStream(input);

    return new BitmapDrawable(x);
}

And then just define the ownership of the ImageButton:

btn.setImageResource(drawableFromUrl("http://..."));

I haven’t tested it, but you’ll probably have to do it in a thread other than UI.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.