2011-10-04 12 views
16

ho voluto impostare il layout lineare sfondo dinamico nel modo seguente:Set lineare layout di sfondo dinamico

  1. Fetch immagine dal web url attraverso parsing XML e quindi memorizzare tale immagine in scheda SD.

  2. Ora l'immagine viene salvata nella scheda SD.

  3. Imposta l'immagine come sfondo di layout lineare nell'app.

Ora sono bloccato nel terzo passaggio. Qualcuno può aiutare?

+0

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

risposta

44

Utilizzare questa:

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

verificare anche questo: How to convert a Bitmap to Drawable in android?

+0

non salvare la mia immagine nel byte array.am salvandola come InputStream è = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream (is); FileOutputStream fos = new FileOutputStream (outputFile); bmImg.compress (CompressFormat.JPEG, 0, fos); fos.flush(); fos.close(); – Kanika

+0

In questo caso, utilizzare questo: 'bmImg = BitmapFactory.decodeStream (è); '' sfondo BitmaBitmapDrawable = new BitmapDrawable (bmImg); '' linearLayout.setBackgroundDrawable (sfondo); ' – Deimos

+0

no la sua non funziona, ancora mostrandomi un'eccezione di puntatore nullo – Kanika

2

Un modo più semplice:

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

Eccezione puntatore nullo è presente con il codice precedente – Kanika

-2

È anche possibile impostare un'immagine dalla cartella drawable.

yourView.setBackgroundResource(R.drawable.FILENAME); 

Imposta FILENAME come immagine di sfondo.

4

ho fatto in questo modo:

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 a carico immagine di sfondo :

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

Spero che questo ti possa aiutare.

3

API sono deprecati è possibile utilizzare il codice qui sotto

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

tenta di utilizzare questo:

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

Usa @ Deimos, ma in questo modo dal momento che alcuni metodi sono obsoleti ora

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