基本方法:
void loadPictrue(){ try { byte[] data = GetImageForNet.getImage(path); String d = new String(data); int length = data.length; Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length); imageView.setImageBitmap(bitMap); } catch (Exception e) {//Log.i(TAG, e.toString());//Toast.makeText(DataActivity.this, "擷取圖片失敗", 1).show();}}
public static byte[] getImage(String path) throws Exception {URL url = new URL(path);HttpURLConnection httpURLconnection = (HttpURLConnection)url.openConnection();httpURLconnection.setRequestMethod("GET");httpURLconnection.setReadTimeout(6*1000);InputStream in = null;byte[] b = new byte[1024];int len = -1;if (httpURLconnection.getResponseCode() == 200) { in = httpURLconnection.getInputStream(); byte[] result = readStream(in); in.close(); return result; }return null;}public static byte[] readStream(InputStream in) throws Exception{ByteArrayOutputStream outputStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;while((len = in.read(buffer)) != -1) {outputStream.write(buffer, 0, len);}outputStream.close();in.close();return outputStream.toByteArray();}
應用執行個體:
:
package com.sample.fun;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.sample.tools.Tool;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.SimpleAdapter;import android.widget.TextView;public class PPTLoad extends Activity {ListView mListView = null; List<Map<String,Object>> mData= new ArrayList<Map<String,Object>>(); private String[] titles = {"��f��","����","����","�����","������"}; private String[] infos = {"ww1","ww2","ww3","w4","w5"}; private String[] paths = {"http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wang_li_hong.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wu_zun.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/he_run_dong.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/jin_cheng_wu.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wu_yan_zu.jpg", "http://t3.baidu.com/it/u=755374211,3445834761&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=3723968736,667273267&fm=21&gp=0.jpg", "http://t3.baidu.com/it/u=450937786,2820844387&fm=21&gp=0.jpg", "http://t2.baidu.com/it/u=3310624697,1755806991&fm=21&gp=0.jpg", "http://down.tutu001.com/d/file/20110315/1b036918fa58fae13cf625bd40_560.jpg", "http://t2.baidu.com/it/u=2525751618,3563581769&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=2298155874,1881401445&fm=21&gp=0.jpg", "http://t3.baidu.com/it/u=1403032584,1700698811&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=2532175015,2728227933&fm=21&gp=0.jpg", "http://t2.baidu.com/it/u=2297216674,2102514028&fm=21&gp=0.jpg", "http://t2.baidu.com/it/u=2698839426,2064924179&fm=21&gp=0.jpg", "http://t3.baidu.com/it/u=1799912067,1926213209&fm=21&gp=0.jpg", "http://t3.baidu.com/it/u=1204591528,144523060&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=2562853530,2288153626&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=3979343889,4211958307&fm=21&gp=0.jpg", "http://t3.baidu.com/it/u=2166483555,3083761236&fm=21&gp=0.jpg", "http://t2.baidu.com/it/u=2492116836,1113873934&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=1767705907,1330856669&fm=21&gp=0.jpg", "http://img12.3lian.com/gaoqing02/02/48/04.jpg", "http://img1.she.thirsight.com/files/tsd/styles/large/public/201204/20/13349014345346.jpg", "http://down.tutu001.com/d/file/20110606/b4baf9234732a7ff127265ae12_560.jpg", "http://down.tutu001.com/d/file/20100831/c58b6b98cb5409d4ec50b58554_560.jpg", "http://down.tutu001.com/d/file/20100912/c63d1d4cc78479736020cd81a2_560.jpg", "http://t1.baidu.com/it/u=2667010967,1884025688&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=1532980374,2719532859&fm=21&gp=0.jpg", "http://t3.baidu.com/it/u=1456755524,1001384106&fm=21&gp=0.jpg", "http://img1.she.thirsight.com/files/tsd/styles/large/public/201205/27/13381609390113.jpg", "http://t2.baidu.com/it/u=3217099276,4236859882&fm=21&gp=0.jpg", "http://sxbctv.newhua.com/softimg/420000/410006s.jpg", "http://t3.baidu.com/it/u=1281775347,885390158&fm=21&gp=0.jpg", "http://img.sc115.com/uploads/sc/pic/130405/1364794891722.jpg", "http://t2.baidu.com/it/u=945900905,2852462510&fm=21&gp=0.jpg", "http://t1.baidu.com/it/u=1542217356,1581665397&fm=21&gp=0.jpg", "http://www.910club.cn/powerpoint/pptmb/UploadSoftPic/20111008/small24ee82be20e3a0d451fb7aab946d28291314864102.jpg", "http://t3.baidu.com/it/u=403513111,1569426262&fm=21&gp=0.jpg", "http://tuidc4.newhua.com/softimg/270000/261088s.jpg" }; @Overrideprotected void onCreate(Bundle savedInstanceState) {setContentView(R.layout.activity_main);for(int i=0;i<paths.length;i++){Map<String, Object> hm=new HashMap<String, Object>();hm.put("image", paths[i]);hm.put("title", "name"+i);hm.put("info", "info"+i);mData.add(hm);}ImageAdapter adapter=new ImageAdapter(this, mData, R.layout.file_row,new String[] { "image","title", "info"},new int[] { R.id.fileImage, R.id.fileName, R.id.fileInfo});//adapter.setImageUrl(urls)ListView fileList=(ListView)findViewById(R.id.fileList);fileList.setAdapter(adapter);super.onCreate(savedInstanceState);}@Overrideprotected void onDestroy() {super.onDestroy();}class ImageAdapter extends SimpleAdapter{Map<Integer, Bitmap> images=new HashMap<Integer, Bitmap>();//List<Bitmap> images=new ArrayList<Bitmap>();//List<String> imageUrls=null;public ImageAdapter(Context context,List<? extends Map<String, ?>> data, int resource,String[] from, int[] to) {super(context, data, resource, from, to);}/*public void setImageUrl(List urls){this.imageUrls=urls;}*/@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v = super.getView(position, convertView, parent); final HashMap<String, Object> hm = (HashMap<String, Object>) super.getItem(position); final ImageView file_image = (ImageView) v.findViewById(R.id.fileImage);//Log.e("msg", "widget= "+v.findViewById(R.id.pro)); final ProgressBar progress = (ProgressBar) v.findViewById(R.id.pro); final TextView file_name = (TextView) v.findViewById(R.id.fileName); final TextView file_info = (TextView) v.findViewById(R.id.fileInfo);// final String imageurl = imageUrls.get(position); /*final String imageurl = imageUrls.get(position); LayoutInflater inflate = LayoutInflater.from(context); View v = inflate.inflate(rowResId, parent, false); final ImageView image = (ImageView) v.findViewById(R.id.image);*/// final ProgressBar progress = (ProgressBar) v.findViewById(R.id.pro); if(images.get(position) != null) { progress.setVisibility(View.GONE); file_image.setImageBitmap(images.get(position)); file_image.setVisibility(View.VISIBLE); file_name.setText(hm.get("title").toString()); file_info.setText(hm.get("info").toString()); } else { file_image.setVisibility(View.GONE); progress.setVisibility(View.VISIBLE); final int pos=position; final Handler handler = new Handler() { @Override public void handleMessage(Message message) { /* System.out.println("Set image at" + pos + " Image Name :: " + imageurl);*/ progress.setVisibility(View.GONE); file_image.setImageBitmap((Bitmap) message.obj); file_image.setVisibility(View.VISIBLE); file_name.setText(hm.get("title").toString()); file_info.setText(hm.get("info").toString()); } }; Thread thread = new Thread() { @Override public void run() { Bitmap drawable = LoadImageFromWebOperations(hm.get("image").toString()); images.put(pos, drawable); Message message = handler.obtainMessage(1, drawable); handler.sendMessage(message); } }; thread.start(); } return v;}private Bitmap LoadImageFromWebOperations(String imageurl) {byte[] data=getImage(imageurl);Bitmap image=BitmapFactory.decodeByteArray(data, 0, data.length);return image;}public byte[] getImage(String path){URL url=null;try {url = new URL(path);} catch (MalformedURLException e2) {e2.printStackTrace();Log.e(getClass().getName(), Tool.getExceptionMessage(e2));}HttpURLConnection httpURLconnection=null;try {httpURLconnection = (HttpURLConnection)url.openConnection();httpURLconnection.setRequestMethod("GET");httpURLconnection.setReadTimeout(6*1000);} catch (IOException e1) {e1.printStackTrace();Log.e(getClass().getName(), Tool.getExceptionMessage(e1));}InputStream in = null;byte[] result=null;try{if (httpURLconnection.getResponseCode() == 200) { in = httpURLconnection.getInputStream(); result = readStream(in); in.close();}}catch(IOException e){e.printStackTrace();Log.e(getClass().getName(), Tool.getExceptionMessage(e));}return result;}public byte[] readStream(InputStream in){ByteArrayOutputStream outputStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = -1;try{while((len = in.read(buffer)) != -1) {outputStream.write(buffer, 0, len);}outputStream.close();in.close();}catch(IOException e){e.printStackTrace();Log.e(getClass().getName(), Tool.getExceptionMessage(e));}return outputStream.toByteArray();}}}
相關許可權:
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name = "android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
附加listview最佳化相關文章
ListView中getView的原理+如何在ListView中放置多個item
http://www.cnblogs.com/xiaowenji/archive/2010/12/08/1900579.html
Android開發之ListView適配器(Adapter)最佳化
http://shinfocom.iteye.com/blog/1231511