2011-10-04 10 views
16

quería poner el fondo Disposición lineal dinámica de la siguiente manera:Set lineal fondo de diseño dinámicamente

  1. Fetch imagen de la URL web a través de análisis de XML y luego almacenar esa imagen en la tarjeta SD.

  2. Ahora la imagen guardada en una tarjeta sd.

  3. Establezca esa imagen como fondo de diseño lineal en la aplicación.

Ahora estoy atascado en el tercer paso. ¿Alguien puede ayudar?

+0

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

Respuesta

44

Utilice esta:

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

También marque esta: How to convert a Bitmap to Drawable in android?

+0

no guardo mi imagen en byte array.am guardándola como InputStream is = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream (es); FileOutputStream fos = new FileOutputStream (archivo de salida); bmImg.compress (CompressFormat.JPEG, 0, fos); fos.flush(); fos.close(); – Kanika

+0

En este caso, use esto: 'bmImg = BitmapFactory.decodeStream (es); ' ' BitmaBitmapDrawable background = new BitmapDrawable (bmImg); ' ' linearLayout.setBackgroundDrawable (background); ' – Deimos

+0

no está funcionando, todavía me muestra la excepción de puntero nulo – Kanika

2

Una forma más fácil:

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

Excepción de puntero nulo está allí con el código anterior – Kanika

-2

También puede establecer una imagen de la carpeta estirable.

yourView.setBackgroundResource(R.drawable.FILENAME); 

Que establece FILENAME como imagen de fondo.

4

que han hecho de esta manera:

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 carga imagen de fondo :

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

Espero que esto te ayude.

3

API están en desuso se puede usar el siguiente código

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

intenta utilizar esto:

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

Uso de @ Deimos, pero como esto ya que algunos métodos son obsoletos ahora

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