Android 从网上下载图片并显示到ListView上

首先我们要准备好图片的数据,就是一些网站上图片的地址

拿到之后就可以直接来写了



package com.example.androidnet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

public class ListViewShowPic extends Activity {

	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_listview);

		listView = (ListView) findViewById(R.id.lv_show_img);

		MyAdapter adapter = new MyAdapter(this);
		listView.setAdapter(adapter);
		Log.d("logd", "--UI--");
	}

	class MyAdapter extends BaseAdapter {

		/**
		 * 网上的图片url
		 */
		String[] picUrls = new String[] {
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/3e1acdd6ecdf4dc2aa9abc45be2443db.jpg",
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/cff4bd31c3934c8388cd5b47d3d34e29.jpg",
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/4fd9f3557acd4fb28b28af7a89a2ff27.jpg",
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/7ead2966291e45c4aef8a198f127bd4a.jpg",
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/245c7655f4de4536a539a559c9434ce4.jpg",
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/51b047314ca74259839c4e6e666e9c71.jpg",
				"http://bmob-cdn-1178.b0.upaiyun.com/2016/05/10/1a11e2a135bf4bc29fd60831c64f558c.jpg" };
		/**
		 * 名称对应url上的图片名称
		 */
		String[] picNames = new String[] {
				"3e1acdd6ecdf4dc2aa9abc45be2443db.jpg",
				"cff4bd31c3934c8388cd5b47d3d34e29.jpg",
				"4fd9f3557acd4fb28b28af7a89a2ff27.jpg",
				"7ead2966291e45c4aef8a198f127bd4a.jpg",
				"245c7655f4de4536a539a559c9434ce4.jpg",
				"51b047314ca74259839c4e6e666e9c71.jpg",
				"1a11e2a135bf4bc29fd60831c64f558c.jpg" };
		String path = Environment.getExternalStorageDirectory()
				+ "/listviewImg/";// 文件目录

		LayoutInflater layoutInflater;
		Context context;
		File fileDir;

		public MyAdapter(Context context) {
			this.context = context;
			layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


			/**
			 * 文件目录如果不存在,则创建
			 */
			fileDir = new File(path);
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}

		}

		@Override
		public int getCount() {
			return picUrls.length;
		}

		@Override
		public Object getItem(int position) {
			return picUrls[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View view = layoutInflater.inflate(R.layout.listview_img, null);

			ImageView imageView = (ImageView) view.findViewById(R.id.img);

			/**
			 * 创建图片文件
			 */
			File file = new File(fileDir, picNames[position]);
			if (!file.exists()) {// 如果本地图片不存在则从网上下载
				downloadPic(picNames[position], picUrls[position]);
			} else {// 图片存在则填充到listview上
				Bitmap bitmap = BitmapFactory
						.decodeFile(file.getAbsolutePath());
				imageView.setImageBitmap(bitmap);
			}
			return view;
		}

		/**
		 * 使用子线程下载图片
		 * 
		 * @param name
		 * @param url
		 */
		private void downloadPic(final String name, final String picurl) {

			new Thread(new Runnable() {

				@Override
				public void run() {

					FileOutputStream fos = null;
					InputStream in = null;

					// 创建文件
					File file = new File(fileDir, name);

					try {

						fos = new FileOutputStream(file);

						URL url = new URL(picurl);

						in = url.openStream();

						int len = -1;
						byte[] b = new byte[1024];
						while ((len = in.read(b)) != -1) {
							fos.write(b, 0, len);
						}

					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
						try {
							if (fos != null) {
								fos.close();
							}
							if (in != null) {
								in.close();
							}
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}).start();

		}

	}
}



你可能感兴趣的:(android,ListView,图片)