Android: View image from the web
Simple example on how to get and display image from the web without saving it to local storage in Android.
Download the source: Example1.zip
package org.asantoso; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; public class Example1 extends Activity{ EditText inputUrl; OnClickListener getImageBtnOnClick = new OnClickListener() { public void onClick(View view) { Context context = view.getContext(); Editable ed = inputUrl.getText(); Drawable image = ImageOperations(context,ed.toString(),"image.jpg"); ImageView imgView = new ImageView(context); imgView = (ImageView)findViewById(R.id.image1); imgView.setImageDrawable(image); } }; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); inputUrl = ((EditText)findViewById(R.id.imageUrl)); inputUrl.setSingleLine(); inputUrl.setTextSize(11); Button getImageButton = (Button)findViewById(R.id.getImageButton); getImageButton.setOnClickListener(getImageBtnOnClick); } private Drawable ImageOperations(Context ctx, String url, String saveFilename) { try { InputStream is = (InputStream) this.fetch(url); Drawable d = Drawable.createFromStream(is, "src"); return d; } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } } public Object fetch(String address) throws MalformedURLException,IOException { URL url = new URL(address); Object content = url.getContent(); return content; } }
About this entry
You’re currently reading “Android: View image from the web,” an entry on simple blog
- Published:
- March 7, 2008 / 1:33 pm
- Category:
- Android
- Tags:
- Android, stream image, view image
92 Comments
Jump to comment form | comment rss [?] | trackback uri [?]