Sep 9, 2013

Html.ImageGetter load image from internet

Last example "Display HTML String with multi images on TextView, with Html.ImageGetter" load image from local drawable. This example demonstrate how to implement Html.ImageGetter() to load image from internet, and also how to handle images from difference source, local drawable and internet.

Html.ImageGetter load image from internet
Html.ImageGetter load image from internet


package com.example.androidhtmltextview;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

 String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='http://android-coding.blogspot.com'>Android Coding</a></b>"
   + "<br/><br/>"
   + "another image load from internet<br/>"
   + "<img src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_vBn5OxsMM1fUIK0w8awJdKJjgJJ07cBYdWcCscYMujdoEYWWi549p7J5FYnYJSggLjw2pomiwcseExrMX6X8OarAb-bEQwH-sccJtSyHlGnoUrrVtIbgbXP_60n3nzhAwAA1mMuXoNRf/s400/AndroidHtmlTextView_multi_images.png'><br/>";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  TextView htmlTextView = new TextView(this);
  setContentView(htmlTextView);

  htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter() {

   @Override
   public Drawable getDrawable(String source) {

    Toast.makeText(getApplicationContext(), source,
      Toast.LENGTH_LONG).show();

    Drawable drawable = null;
    if (source.startsWith("http")) {
     // load from internet

     URL sourceURL;
     try {
      sourceURL = new URL(source);
      URLConnection urlConnection = sourceURL.openConnection();
      urlConnection.connect();
      InputStream inputStream = urlConnection.getInputStream();
      BufferedInputStream bufferedInputStream = 
        new BufferedInputStream(inputStream);
      Bitmap bm = BitmapFactory.decodeStream(bufferedInputStream);

      // convert Bitmap to Drawable
      drawable = new BitmapDrawable(getResources(), bm);
      
      drawable.setBounds(0, 0, 
        bm.getWidth(),
        bm.getHeight());

     } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

    } else {
     // load from local drawable

     int dourceId = getApplicationContext()
       .getResources()
       .getIdentifier(source, "drawable", getPackageName());

     drawable = getApplicationContext().getResources()
       .getDrawable(dourceId);

     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
       drawable.getIntrinsicHeight());
    }

    return drawable;
   }

  }, null));

  htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());

 }

}


In order to load image from internet, permission of "android.permission.INTERNET" have to be added in AndroidManifest.xml.

To solve error of android.os.NetworkOnMainThreadException, read Html.ImageGetter load image from internet, in AsyncTask.

4 comments:

  1. It gives android.os.NetworkOnMainThreadException. How can be fixed this ?

    ReplyDelete
    Replies
    1. Please read http://android-coding.blogspot.com/2014/10/htmlimagegetter-load-image-from.html

      Delete
  2. Your code is still relevant even in 2017. Thanks a lot!!!
    Took some working to tailor it to my app but it's good to go now. God bless you

    ReplyDelete
  3. In my app i used this code but i got only image url not image what should i do ?

    ReplyDelete

Infolinks In Text Ads