Here is my code to fetch image from an
url and then display it into imageview in android (I use android 2.2.3)
In this tutorial, I get image from this
link https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizHvU0XODfjwRifjEZqSlbL7tLlgLVsEjUmgQnLIdtud9kr_Fm8NADiZeF-ajC5-7Um8za4iMM6mXwRAzHPGhyphenhyphenMIc_LCekHNtrDkouUI2SXospNb8DjkEAWDkPUqDBl8TIitJZ_MuTZ3c/h120/images.png
- main.xml layout:
public static String src_link = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizHvU0XODfjwRifjEZqSlbL7tLlgLVsEjUmgQnLIdtud9kr_Fm8NADiZeF-ajC5-7Um8za4iMM6mXwRAzHPGhyphenhyphenMIc_LCekHNtrDkouUI2SXospNb8DjkEAWDkPUqDBl8TIitJZ_MuTZ3c/h120/images.png"; public ImageView imgView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgView = (ImageView)findViewById(R.id.imageView1); imgView.setImageBitmap(fetchImage(src_link)); } private Bitmap fetchImage(String urlstr) { Bitmap img = null; try { URL url; url = new URL(urlstr); HttpURLConnection c = (HttpURLConnection) url.openConnection(); c.setDoInput(true); c.connect(); InputStream is = c.getInputStream(); img = BitmapFactory.decodeStream(is); return img; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return img; }
- Finally, add permission in Manifest.xml:
Yep! This is end of this topic. Hope you enjoy it! :)
No comments:
Post a Comment