package com.apofa.httpAccess; import java.io.BufferedInputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class httpAccess extends Activity { /** Called when the activity is first created. */ ImageView displayView; Button downloadImage; Bitmap bm; String imageurl = "http://www.leckerfett.com/napf/andesk.png"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); downloadImage= (Button)findViewById(R.id.getimage); displayView = (ImageView)findViewById(R.id.imageview); downloadImage.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // displayImage(downloadImage(imageurl)); Downloader dm= new Downloader(); dm.execute(imageurl); } }); } public void displayImage(Bitmap bm){ BitmapDrawable bmdw = new BitmapDrawable(bm); displayView.setBackgroundDrawable(bmdw); } public Bitmap downloadImage(String url){ try{ URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); //Log.d("LOADIMAGE","GET STREAM "); InputStream is = conn.getInputStream(); //Log.d("LOADIMAGE","BUFFER STREAM"); /* Buffered is always good for a performance plus. */ BufferedInputStream bis = new BufferedInputStream(is); Bitmap bm = BitmapFactory.decodeStream(bis); return bm; } catch(Exception e){ Log.d("httpAccess", "message:"+e.toString()); return null; } } private class Downloader extends AsyncTask { protected Bitmap doInBackground(String... searchKey) { String url = searchKey[0]; try{ Log.v("gsearch","gsearch result with AsyncTask"); return downloadImage(url); }catch(Exception e){ Log.v("Exception google search","Exception:"+e.getMessage()); return null; } } protected void onPostExecute(Bitmap resultbm) { try{ displayImage(resultbm); }catch(Exception e){ Log.v("Exception google search","Exception:"+e.getMessage()); } } } }