Senin, 09 September 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/AVvXsEhXxPwiiL0VxZgedR8ncfAJBm9RTzqJQyUUF6V-6dgES5K0u6KiUo5Iku2L4AcO6g492tfVTOtOzaQudk0HeA2l0vRGylzLhjxmTOpc7YXTyM_tEAurzylq5uY4d6fwcbel-RzIfRb9xMWo/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.

0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved