Load image from internet, in AsyncTask
This example show how to load image from internet, in background thread using AsyncTask.Load image from internet |
<uses-permission android:name="android.permission.INTERNET"/>
- You cannot access Network in main thread, otherwise error of "android.os.NetworkOnMainThreadException" will be reported. That's why we have to implement AsyncTask to load image in background thread.
- Once image loaded, update UI in main thread (or called UI Thread) in onPostExecute() method.
Example code:
package com.example.androidloadimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
String targetUrl = "http://goo.gl/qfGTN3";
ImageView image1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView)findViewById(R.id.image1);
/*
* error Caused by: android.os.NetworkOnMainThreadException
* if you access internet in Main Thread here!
*/
//Bitmap bmLoadedImage = loadImageFromUrl(targetUrl);
//image1.setImageBitmap(bmLoadedImage);
new LoadImageTask(image1).execute(targetUrl);
}
private class LoadImageTask extends AsyncTask<String, Void, Bitmap>{
ImageView targetImageView;
LoadImageTask(ImageView iv){
targetImageView = iv;
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bm = loadImageFromUrl(params[0]);
return bm;
}
@Override
protected void onPostExecute(Bitmap result) {
targetImageView.setImageBitmap(result);
}
}
private Bitmap loadImageFromUrl(String targetUrl){
Bitmap bm = null;
try {
URL url = new URL(targetUrl);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
bm = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bm;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
0 komentar:
Posting Komentar