2011-10-04 9 views
16

Chciałem ustawić Układ normalny tła dynamicznie w następujący sposób:Zestaw liniowy układ tła dynamicznie

  1. Fetch obraz z URL internetowej przez parsowania XML, a następnie zapisać, że obraz na karcie SD.

  2. Teraz obraz zapisany na karcie SD.

  3. Ustaw ten obraz jako tło układu liniowego w aplikacji.

Teraz utknąłem w trzecim kroku. Czy ktoś może pomóc?

+0

http://developerlife.com/tutorials/?p=312 –

Odpowiedz

44

Użyj tego:

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

Zobacz również: How to convert a Bitmap to Drawable in android?

+0

Nie zapisuję mojego obrazu do tablicy array.am zapisując go jak InputStream is = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream (jest); FileOutputStream fos = new FileOutputStream (outputFile); bmImg.compress (CompressFormat.JPEG, 0, fos); fos.flush(); fos.close(); – Kanika

+0

W tym przypadku użyj tego: 'bmImg = BitmapFactory.decodeStream (is); ' ' BitmaBitmapDrawable background = new BitmapDrawable (bmImg); ' ' linearLayout.setBackgroundDrawable (background); ' – Deimos

+0

to nie działa, wciąż pokazuje mi wyjątek wskaźnika zerowego – Kanika

2

Łatwiejszy sposób:

BitmapDrawable d = new BitmapDrawable("/sdcard/data/image.jpg"); 
linearLayout.setBackgroundDrawable(d); 
+0

Wyjątek wskaźnika pustego jest z powyższym kodem – Kanika

-2

Można również ustawić obraz z rozciągliwej folderu.

yourView.setBackgroundResource(R.drawable.FILENAME); 

Ustaw jako FILENAME jako tło.

4

mam zrobić w ten sposób:

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 do obciążenie obraz w tło:

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); 
    } 
} 

Mam nadzieję, że to ci pomoże.

3

API są nieaktualne można użyć poniższy kod

BitmapDrawable background = new BitmapDrawable(getResources(), bitmapImage); 
linearLayout.setBackground(background); 
0

spróbuje użyć tego:

Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), R.drawable.img); 
BitmapDrawable bmpBackground = new BitmapDrawable(getResources(), bmpOriginal) 
0
odpowiedź

Przymież @ Deimos, ale tak, ponieważ niektóre metody są przestarzałe teraz

Bitmap bmImg = BitmapFactory.decodeStream(is); 
BitmapDrawable background = new BitmapDrawable(context.getResources(), bmImg); 
linearLayout.setBackground(background); 
Powiązane problemy