19

I wanted to set Linear Layout background dynamically in the following way:

  1. Fetch image from web url through XML parsing and then store that image into sd card.

  2. Now the image saved into sd card.

  3. Set that image as a linear layout background in the app.

Now I am stuck in the third step. Can anyone help?

thegrinner
  • 11,546
  • 5
  • 41
  • 64
Kanika
  • 10,648
  • 18
  • 61
  • 81

7 Answers7

51

Use this:

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(bmImg);
linearLayout.setBackgroundDrawable(background);

Also check this: How to convert a Bitmap to Drawable in android?

Community
  • 1
  • 1
Deimos
  • 1,083
  • 1
  • 10
  • 15
  • am not saving my image into byte array.am saving it like InputStream is = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream(is); FileOutputStream fos = new FileOutputStream(outputFile); bmImg.compress(CompressFormat.JPEG, 0, fos); fos.flush(); fos.close(); – Kanika Oct 04 '11 at 11:23
  • In this case use this: `bmImg = BitmapFactory.decodeStream(is);` `BitmaBitmapDrawable background = new BitmapDrawable(bmImg);` `linearLayout.setBackgroundDrawable(background);` – Deimos Oct 04 '11 at 11:27
  • no its not working ,it still showing me null pointer exception – Kanika Oct 04 '11 at 11:34
  • 1
    Are you sure the image is being downloaded and saved properly? Does it exist? – Deimos Oct 04 '11 at 11:35
  • yes i can see the image at sd card,I dnt know wheres the problem in the code – Kanika Oct 04 '11 at 11:45
  • Well it's hard to tell what went wrong. Try to add `bmImg.compress(CompressFormat.JPEG, 0, fos);` before creating `BitmapDrawable background = new BitmapDrawable(bmImg);` – Deimos Oct 04 '11 at 11:54
  • yes,thank you now it is showing me the background image..thanx alot – Kanika Oct 04 '11 at 12:06
  • This require API 16. Is there other way for lower API? – Marek Jun 05 '13 at 01:00
  • As far as I recall this doesn't require API 16. – Deimos Jun 18 '13 at 10:56
  • 2
    BitmapDrawable is deprecated. – user1940676 May 26 '14 at 08:39
  • 1
    `new BitmapDrawable(bmImg)` is deprecated. Use `new BitmapDrawable(getContext().getResources(), bmImg)` instead. – Janneman96 Sep 09 '16 at 09:44
5

I have done this way:

private RelativeLayout relativeLayout;

onCreate:

relativeLayout= (RelativeLayout)findViewById(R.id.relativeLayout);

new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
            "androidfigure").execute();

AsyncTask to load image in background:

private class LoadBackground extends AsyncTask<String, Void, Drawable> {

    private String imageUrl , imageName;

    public LoadBackground(String url, String file_name) {
        this.imageUrl = url;
        this.imageName = file_name;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Drawable doInBackground(String... urls) {

        try {
            InputStream is = (InputStream) this.fetch(this.imageUrl);
            Drawable d = Drawable.createFromStream(is, this.imageName);
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    private Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

    @Override
    protected void onPostExecute(Drawable result) {
        super.onPostExecute(result);
        relativeLayout.setBackgroundDrawable(result);
    }
}

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
4

API are deprecated you can use the below code

BitmapDrawable background = new BitmapDrawable(getResources(), bitmapImage);
linearLayout.setBackground(background);
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
4

An easier way:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg");
linearLayout.setBackgroundDrawable(d);
kazufukurou
  • 166
  • 5
0

Try to use this:

Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.img);
BitmapDrawable bmpBackground = new BitmapDrawable(getResources(), bmpOriginal)
Floern
  • 33,559
  • 24
  • 104
  • 119
0

Use @Deimos's answer, but like this since some methods are deprecated now

Bitmap bmImg = BitmapFactory.decodeStream(is);
BitmapDrawable background = new BitmapDrawable(context.getResources(), bmImg);
linearLayout.setBackground(background);
-1

You can also set image from drawable folder.

yourView.setBackgroundResource(R.drawable.FILENAME);

That set FILENAME as background image.

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
Sunil_Suthar
  • 1,094
  • 1
  • 10
  • 20