Como pasar de Drawable a Bitmap y descargar imagen de internet a Bitmap

Objetivo:

Pasar de Drawable a Bitmap, o descargar como Bitmap una imagen de internet a través de su url.

Para ello podemos usar las funciones que hay más abajo, llamadas drawableToBitmap(Drawable mDrawable) y urlImageToBitmap(String urlImage), donde el valor devuelto de cada una de las funciones será de la clase Bitmap.

A continuación mostramos la primera función, a la cual le pasamos la instancia del objeto Drawable para que nos devuelva el Bitmap:

Código fuente función (Java):

[sourcecode language=”java”]
/**
* Pasar de Drawable a Bitmap. By TutorialAndroid.com
* @param Drawable mDrawable
* @return Bitmap
*/
public Bitmap drawableToBitmap(Drawable mDrawable){
// Usamos el método decode Resource de BitmapFactory para la conversión
return BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource);
}
[/sourcecode]

A continuación mostramos la primera función, a la cual le pasamos la instancia del objeto Drawable para que nos devuelva el Bitmap:

Código fuente función (Java):

[sourcecode language=”java”]
/**
* Descarga una imagen de internet a Bitmap. By TutorialAndroid.com
* @param String urlImage
* @return Bitmap (null en caso de no poder)
*/
public Bitmap urlImageToBitmap(String urlImage){
Bitmap mIcon1 = null;
URL url_value = new URL(urlImage);
if (url_value != null) {
mIcon1 = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
}
return mIcon1;
}
[/sourcecode]

Una vez hemos obtenido el Bitmap, podemos realizar tareas con él, tal y como cambiar el tamaño (ver aqui).

Espero que os haya servido de ayuda.

¡Un saludo!

6 comentarios en “Como pasar de Drawable a Bitmap y descargar imagen de internet a Bitmap

  1. Excelente explicación estoy metiendome recién en esto de android, me queda una duda en relación a la segunda función en la línea 9, ya que no se ha declarado profile ¿? a qué tipo de variable hace alusión ya que si trabajo usando la función me pide que use posteriormente un try catch.

    desde ya gracias

  2. 鈥?Straight protection, the location where the center takes http://www.bestlions.com/Matthew_Stafford_Jersey_MS181 one blitzer additionally the other is allowed a free of charge release to your running back, who must think of a key block during the quarterback’s lap. “This blitz changed Suh Jersey what you require from a running back,” says Bugel. “He’s has to discover ways to pass-block, course really depends on really can’t get him to on the field.”.
    Yes, I picking someone out of the Redskins, whose offense will now be being overseen using guy who was calling bingo games in a senior center fortnight ago (Really! You will for details.). Campbell in addition to the host the terrible Chiefs recently, but more Peterson Jersey importantly, he not a bad backup for the long term in the process. With six scoring passes and 221 yards per game solar power, he been solid.
    simply in this article at the gambling mean

    Other Information about This Articles:

    http://www.smileoutreach.org/node/4516
    http://crossmyheartandhopetolive.com/?q=node/5377
    http://teleshev.ru/forum/topic.php?id=8805&replies=1#post-11497
    http://robot.stuffination.com/forum/topic.php?id=342843&replies=1#post-362623
    http://legallygetfreeelectric.com/groups/chicago-cubs-vs-ny-yankees-qiukaikai1112/

  3. Why? Because to look at told an individual about my intentions to exit I knew I would personally have Woodson Jersey a single choice but to set up my small business. There’d be no turning back. That you can use which should be accountable that you purchase any time you tell someone what your primary goal are.
    I’ve truly nothing against Schottenheimer arriving to Miami since OC. Just didn want him becoming HC. Just our I am he was the scapegoat all season http://www.packers-shop.com/21_Charles_Woodson relating to the teams struggles. Grinnell College could be a miserable little place a remote little hick town labeled as Grinnell Iowa. On at times, if your wind is blowing accurate, someone can catch the stench of Hog Factories wafting all around the soybean and cornfields. A freight train carrying many gallons of corn syrup (70% of the US supply) passes directly within the middle of http://www.bearssportsshop.com/1007BM_Brandon_Marshall_Jersey campus several times a day a day..

    You will get more you like follow the links below:

    http://www.markusworks.com/?q=node/85876
    http://www.galilaia.com/member/blog_post_view.php?postId=146164
    http://black-on.myforum.ro/viewtopic.php?p=954#95
    http://shiidea.mywapblog.com/mixed-martial-arts-career-fahfieufak2012.xhtml
    http://www.vflw.net/forum/topic.php?id=16961&replies=1#post-28723

Responder a Fertiapinna Cancelar la respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *