<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jon Segador &#187; cargar</title>
	<atom:link href="http://jonsegador.com/tag/cargar/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonsegador.com</link>
	<description>Desarrollador web y android, con todo lo que ello implica</description>
	<lastBuildDate>Wed, 16 May 2012 08:34:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Mostrar/cargar imagen externa en una aplicación Android</title>
		<link>http://jonsegador.com/2010/03/mostrarcargar-imagen-externa-en-una-aplicacion-android/</link>
		<comments>http://jonsegador.com/2010/03/mostrarcargar-imagen-externa-en-una-aplicacion-android/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 13:52:30 +0000</pubDate>
		<dc:creator>Jon</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Desarrollo]]></category>
		<category><![CDATA[cargar]]></category>
		<category><![CDATA[imagen]]></category>
		<category><![CDATA[mostrar]]></category>
		<category><![CDATA[servidor]]></category>

		<guid isPermaLink="false">http://jonsegador.com/?p=176</guid>
		<description><![CDATA[Muchas veces nos encontramos con el problema de tener que cargar imagenes, que tenemos alojadas en un servidor externo, en nuestra aplicación Android. Las imágenes básicas las podemos incluir en la propia aplicación pero imágenes como, por ejemplo, los avatares de los usuarios los tenemos que cargar directamente desde el servidor. Primero creamos el layout [...]]]></description>
			<content:encoded><![CDATA[<p>Muchas veces nos encontramos con el problema de tener que cargar imagenes, que tenemos alojadas en un servidor externo, en nuestra aplicación Android. Las imágenes básicas las podemos incluir en la propia aplicación pero imágenes como, por ejemplo, los avatares de los usuarios los tenemos que cargar directamente desde el servidor.</p>
<p>Primero creamos el layout (main.xml):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span> <span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>LinearLayout xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span>
    android<span style="color: #339933;">:</span>orientation<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;vertical&quot;</span>
    android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span>
    android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fill_parent&quot;</span><span style="color: #339933;">&gt;</span> 
&nbsp;
 <span style="color: #339933;">&lt;</span>ImageView android<span style="color: #339933;">:</span>id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@+id/image_view&quot;</span>
    android<span style="color: #339933;">:</span>layout_width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span>
    android<span style="color: #339933;">:</span>layout_height<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;wrap_content&quot;</span> <span style="color: #339933;">/&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>LinearLayout<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Ahora creamos la activity que cargará la imagen del servidor y la mostrará en el layout anterior (Main.java):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.jonsegador.examples.externalimage</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.HttpURLConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Activity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.Bitmap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.BitmapFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ImageView</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.Toast</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> ImageView imageView<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Bitmap loadedImage<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> imageHttpAddress <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://jonsegador.com/wp-content/apezz.png&quot;</span><span style="color: #339933;">;</span>            
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onCreate<span style="color: #009900;">&#40;</span>Bundle savedInstanceState<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onCreate</span><span style="color: #009900;">&#40;</span>savedInstanceState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setContentView<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">layout</span>.<span style="color: #006633;">main</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        imageView <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ImageView<span style="color: #009900;">&#41;</span> findViewById<span style="color: #009900;">&#40;</span>R.<span style="color: #006633;">id</span>.<span style="color: #006633;">image_view</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       
        downloadFile<span style="color: #009900;">&#40;</span>imageHttpAddress<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">void</span> downloadFile<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> imageHttpAddress<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">URL</span> imageUrl <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            imageUrl <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>imageHttpAddress<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">HttpURLConnection</span> conn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span> imageUrl.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            conn.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            loadedImage <span style="color: #339933;">=</span> BitmapFactory.<span style="color: #006633;">decodeStream</span><span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            imageView.<span style="color: #006633;">setImageBitmap</span><span style="color: #009900;">&#40;</span>loadedImage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Toast.<span style="color: #006633;">makeText</span><span style="color: #009900;">&#40;</span>getApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #0000ff;">&quot;Error cargando la imagen: &quot;</span><span style="color: #339933;">+</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, Toast.<span style="color: #006633;">LENGTH_LONG</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Por último, incluimos la Activity en el AndroidManifest.xml y damos permisos para acceder a internet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #339933;">?&gt;</span>
<span style="color: #339933;">&lt;</span>manifest xmlns<span style="color: #339933;">:</span>android<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://schemas.android.com/apk/res/android&quot;</span>
      <span style="color: #000000; font-weight: bold;">package</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;com.jonsegador.examples.externalimage&quot;</span>
      android<span style="color: #339933;">:</span>versionCode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span>
      android<span style="color: #339933;">:</span>versionName<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>application android<span style="color: #339933;">:</span>icon<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@drawable/icon&quot;</span> android<span style="color: #339933;">:</span>label<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@string/app_name&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>activity android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;.Main&quot;</span> android<span style="color: #339933;">:</span>label<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;@string/app_name&quot;</span><span style="color: #339933;">&gt;</span>
            <span style="color: #339933;">&lt;</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span>
                <span style="color: #339933;">&lt;</span>action android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.intent.action.MAIN&quot;</span> <span style="color: #339933;">/&gt;</span>
                <span style="color: #339933;">&lt;</span>category android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.intent.category.LAUNCHER&quot;</span> <span style="color: #339933;">/&gt;</span>
            <span style="color: #339933;">&lt;/</span>intent<span style="color: #339933;">-</span>filter<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;/</span>activity<span style="color: #339933;">&gt;</span>    
    <span style="color: #339933;">&lt;/</span>application<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>uses<span style="color: #339933;">-</span>sdk android<span style="color: #339933;">:</span>minSdkVersion<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;3&quot;</span> <span style="color: #339933;">/&gt;</span>  
    <span style="color: #339933;">&lt;</span>uses<span style="color: #339933;">-</span>permission android<span style="color: #339933;">:</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;android.permission.INTERNET&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>manifest<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Y este debería de ser el resultado:<br />
<img src="http://jonsegador.com/wp-content/imagen_externa_android-199x300.png" alt="imagen_externa_android" title="imagen_externa_android" width="199" height="300" class="alignnone size-medium wp-image-180" /></p>
]]></content:encoded>
			<wfw:commentRss>http://jonsegador.com/2010/03/mostrarcargar-imagen-externa-en-una-aplicacion-android/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

