Http - Baixar uma imagem |
Top Previous Next |
Tela BaixarActivity.java
import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView;
public class BaixarActivity extends Activity implements OnClickListener, Runnable {
// handler usado para atualizar a view private Handler handler = new Handler(); private ProgressDialog dialog; private Bitmap bitmap;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
Button btBaixar = (Button) findViewById(R.id.btBaixar); btBaixar.setOnClickListener(this); }
public void onClick(View arg0) {
dialog = ProgressDialog.show(this, "Exemplo", "Buscando imagem, aguarde...", false, true); // faz o download numa thread new Thread(this).start(); }
public void run() {
try { Http http = new Http(); // link para imagem bitmap = http.downloadImagem("http://www.market.com.br/img/womansuporte.png"); // precisa do handler para atualizar a view de outra thread handler.post(new Runnable() { public void run() { ImageView imageView1 = (ImageView) findViewById(R.id.imageView1); Drawable d = new BitmapDrawable(bitmap); imageView1.setImageDrawable(d); } }); } catch (Throwable e) { Log.e("Erro", e.getMessage(), e); } finally { dialog.dismiss(); } } }
Http.java
package com.baixar;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log;
public class Http {
public final String downloadArquivo(String url) { Log.i("TESTE", "Http.Download: " + url); try { URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); // configura get conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(false); conn.connect(); InputStream in = conn.getInputStream(); // arquivo String arquivo = readString(in); conn.disconnect(); return arquivo; } catch (Exception e) { Log.e("Erro", e.getMessage()); } return null; }
public final Bitmap downloadImagem(String url) { Log.i("TESTE", "Http.Download: " + url); try { URL u = new URL(url); InputStream in = u.openStream(); Bitmap img = BitmapFactory.decodeStream(in); in.close(); return img; } catch (Exception e) { Log.e("Erro", e.getMessage()); } return null; }
private byte[] readBytes(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { bos.write(buffer, 0, len); }
byte[] bytes = bos.toByteArray(); return bytes; } finally { bos.close(); in.close(); } }
private String readString(InputStream in) throws IOException { byte[] bytes = readBytes(in); String texto = new String(bytes); Log.e("Retorno", texto); return texto; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >
<Button android:id="@+id/btBaixar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Baixar arquivo texto" />
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
Manifest
<uses-permission android:name="android.permission.INTERNET" /> |