Html.ImageGetter load image from internet, in AsyncTask - Hallo sahabat Teknologi Terbaru, Pada Artikel yang anda baca kali ini dengan judul Html.ImageGetter load image from internet, in AsyncTask, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.
Judul : Html.ImageGetter load image from internet, in AsyncTask
link : Html.ImageGetter load image from internet, in AsyncTask
Html.ImageGetter load image from internet, in AsyncTask
This example code use Html.ImageGetter load image from internet, in AsyncTask, to solve error of android.os.NetworkOnMainThreadException in the old example 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.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public class HttpGetDrawableTask extends AsyncTask<String, Void, Drawable> {
TextView taskTextView;
String taskHtmlString;
HttpGetDrawableTask(TextView v, String s) {
taskTextView = v;
taskHtmlString = s;
}
@Override
protected Drawable doInBackground(String... params) {
Drawable drawable = null;
URL sourceURL;
try {
sourceURL = new URL(params[0]);
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();
}
return drawable;
}
@Override
protected void onPostExecute(Drawable result) {
final Drawable taskDrawable = result;
if (taskDrawable != null) {
taskTextView.setText(Html.fromHtml(taskHtmlString,
new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
return taskDrawable;
}
}, null));
}
}
}
TextView htmlTextViewRemote;
String htmlStringRemote = "Image load from internet"
+ "<img src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9R2UggUKqVFUfmHWfKpfBF693eHxP2nq2hB2X2WQMY-jS7EMpAV6KDf5TrH9CKYVF8_xSJYa_r2pKf_3cz7-tiaUkBJZWDsfH0CgrkEXlpdu5ZnOFWvbNJ5TXcS3J76wKwO_palHkMTG1/s400/AndroidHtmlTextView_multi_images.png'>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
htmlTextViewRemote = new TextView(this);
setContentView(htmlTextViewRemote);
htmlTextViewRemote.setText(Html.fromHtml(htmlStringRemote,
new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Toast.makeText(getApplicationContext(), source,
Toast.LENGTH_LONG).show();
HttpGetDrawableTask httpGetDrawableTask = new HttpGetDrawableTask(
htmlTextViewRemote, htmlStringRemote);
httpGetDrawableTask.execute(source);
return null;
}
}, null));
htmlTextViewRemote.setMovementMethod(LinkMovementMethod.getInstance());
}
}
In order to load image from internet, permission of "android.permission.INTERNET" have to be added in AndroidManifest.xml.
Demikianlah Artikel Html.ImageGetter load image from internet, in AsyncTask
Sekianlah artikel Html.ImageGetter load image from internet, in AsyncTask kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
0 Response to "Html.ImageGetter load image from internet, in AsyncTask"
Post a Comment