MainActivity.java
package com.example.androidasynclist;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
final static String src[] = {
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiazjR6a6VxR2GOwKiIXQBpY4OLG2PvkE41VnwZCtKyFWPu6RkWHEeHlETEomqiIi9R2XHaU4MoOWX31pBxjOX8dIqXjeL99C80s1ZdHkkMGkHg_7KKa5iPST9mpsLYimcHxYnxkLURn04/s1600/_01.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTFyD7tQRXGIV_9GO_msd3gAyyqPg3B1y4juZ9jSOCOQGO-PON8A-qxD2dkQ6uCENS6Ad1WhYW0zTimZtSTDlJZNx8NzAE5TcVami3y5c9-3CIU9Jq9HOrTnosUtUkrQoWw0amz9JgN2A/s1600/_02.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQ9bg7a349A5tIhmCK1XMcE_Ec0J11y5xnKdB_fpbIfPWv7YWvbm0epTYFOw_uLUJMvgmSsdWwbgy51MUdJJX3HCpCGzCnD8EinAgQTcRETya3doh1Us8qkHuNyMGwsQltA6MRvb2MFzU/s1600/_03.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9niVOO93U9sBx_JLPxVgTEjpXry-u_mB3X0Skvyqwg3_ki5yX071siueZThAgMRo_gVYsxhsEbBHIf5PMqYEUHrR4gKijeXBWVgLPrpspHAW1F48M35ogII22FET4m2FVMvgIZk57zmg/s1600/_04.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlQE_pmSUbYzFN54FCTxFuLZVvdltylG10E6qC0M1CmI3hZe7JwCXLTKTKqRvcsH-W0ZrkOG5fEpqRyj_TzrmAd7M0Ts4IfnRQdN71uFskAJ1GEAMMhRuTIJKadmSFWxJOF7NB33_GcI8/s1600/_05.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfkJUIsyUyIRcGmouown6jVP0LKgMCMADAQ8oVdOQWSJpLRcMP-iiSBLWczBv_j-dRrAhnByJEAXEhXIbdkTRO9tqRanhkylkaFMc6jvBNtWMdb3e_ApVaWlsf9YggTYaeI5WO2nA0DN8/s1600/_06.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhchIDnqwJGud3VEDJTa6JGSY1NUJuXtAMzhhhELDdXh0t7fW7KAxbshOw8vH8uoJ2wf_fQmOaIvEreqh3HUj2DjF9lhVu2AS2SgVqQ6yQBHuKnaDPuHPhclJi702jPZHJo0VD4RyM-Fro/s1600/_07.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvBNq4KqQe3awmUZySimUu_1MOgTU3I237wczgGWaTMZ7nbtKdBYLDefRAJvMkjl00NNLViNG7UAigTMiXDUq7mpb-n8lKUUdKtueni3iKcsoZWK2fegbu1j1tm2JMI90eXpCyD1CNeQg/s1600/_08.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi-Sze1xjuWync0wJMX9sNpLRQ1t4St1ucjbeCzrrRsS13YJ304gAU1BVvAbex8UznuPRdAulsQkawoObi9UIH5CD9r_hR2O-vQ0OfjKB3UiDb4IzXYptWqLQ1I4ZrmNDtiMVHwLGBMbvQ/s1600/_09.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgddxgk24Zyn2_iTWNxaWQ6WyiAkPKWSloB8kJCGpIh8p4shw8KjS8wNy9UzwAyyxmbOAcLwfedCZVv6h7bf8eHOGpqskjcACBDc-Jdub3IpDl5a7Szi7Qw_KXa8nnbajMGhE_Ai82JuC8/s1600/_10.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiloSt5iL5JWwtl1a0TwcICQ-MTYKqmoEVe6Lqu0YT-PDTybbfsjj-r8eJfE8aaU3_-4xi9FVkCVZYf1wRaLiYZRjzH85oXavIvdSNmuveHHrut90VRfhhpN4Z3E4xQkwF-XAqzGXkgmug/s1600/_11.png",
"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBETB7fVcUfTsv8KswHoQwuYFe99eZxk9bvtijCONUhW3hZiK7VyCCqfIURAElwTvHJmvA_UQKkg_FJjl9D-6VQ-LT6UEN5ltRvm-FRrhRfYpzDoWC67JRxrZUqxOBo04mnfvJm9-1noY/s1600/_12.png" };
ListView imageList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageList = (ListView) findViewById(R.id.imagelist);
ArrayList<String> srcList = new ArrayList<String>(Arrays.asList(src));
imageList.setAdapter(new CustomListAdapter(this, srcList));
}
// ----------------------------------------------------
public class CustomListAdapter extends BaseAdapter {
private ArrayList<String> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<String> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.icon = (ImageView)convertView.findViewById(R.id.icon);
holder.text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(String.valueOf(position));
if (holder.icon != null) {
new BitmapWorkerTask(holder.icon).execute(listData.get(position));
}
return convertView;
}
class ViewHolder {
ImageView icon;
TextView text;
}
}
// ----------------------------------------------------
// Load bitmap in AsyncTask
// ref:
// http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private String imageUrl;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage
// collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
imageUrl = params[0];
return LoadImage(imageUrl);
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
private Bitmap LoadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL)
throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
}
}
/res/layout/activity_main.xml
<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="com.example.androidasynclist.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<ListView
android:id="@+id/imagelist"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
/res/layout/row.xml
<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="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.

Related:
- Load WebP from Internet and display in ListView
0 Response to "Async load image from internet to ListView"
Post a Comment