连网获取图片的小程序

 

这些文件实际存放在电脑的硬盘上,通过tomcat已经做好了映射:参考

  1 package net.blogjava.mobile;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.InputStream;
  5 import java.io.InputStreamReader;
  6 import java.net.URL;
  7 import java.net.URLConnection;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 
 11 import android.app.ListActivity;
 12 import android.content.Context;
 13 import android.content.Intent;
 14 import android.graphics.Bitmap;
 15 import android.graphics.BitmapFactory;
 16 import android.os.Bundle;
 17 import android.util.Log;
 18 import android.view.LayoutInflater;
 19 import android.view.Menu;
 20 import android.view.MenuInflater;
 21 import android.view.MenuItem;
 22 import android.view.View;
 23 import android.view.ViewGroup;
 24 import android.widget.BaseAdapter;
 25 import android.widget.ImageView;
 26 import android.widget.LinearLayout;
 27 import android.widget.RatingBar;
 28 import android.widget.TextView;
 29 
 30 public class Main extends ListActivity
 31 {
 32     
 33     
 34     @Override
 35     public boolean onCreateOptionsMenu(Menu menu) {
 36         
 37     
 38             MenuInflater inflater = getMenuInflater();
 39             inflater.inflate(R.menu.menu, menu);//调用第二步中定义的Menu界面描述文件
 40             return true;
 41 
 42     }
 43     
 44 
 45     @Override
 46     public boolean onOptionsItemSelected(MenuItem item) {
 47         // TODO Auto-generated method stub
 48         Intent intent = new Intent(Main.this, Main.class);
 49         startActivity(intent);                                    
 50          //close this activity
 51          finish();
 52         return true;
 53     }
 54 
 55 
 56     public class ApkListAdapter extends BaseAdapter
 57     {
 58         private Context context;
 59         private LayoutInflater layoutInflater;
 60         private String inflater = Context.LAYOUT_INFLATER_SERVICE;
 61         private String rootUrl = "http://192.168.43.62/apk/";
 62         private String listUrl = rootUrl + "list.txt";
 63         private List<ImageData> imageDataList = new ArrayList<ImageData>();
 64 
 65         class ImageData
 66         {
 67             public String url;
 68             public String applicationName;
 69             public float rating;
 70         }
 71 
 72         private InputStream getNetInputStream(String urlStr)
 73         {
 74             try
 75             {
 76                 URL url = new URL(urlStr);
 77                 URLConnection conn = url.openConnection();
 78                 conn.connect();
 79                 InputStream is = conn.getInputStream();
 80                 return is;
 81             }
 82             catch (Exception e)
 83             {
 84 
 85             }
 86             return null;
 87         }
 88 
 89         public ApkListAdapter(Context context)
 90         {
 91             this.context = context;
 92             layoutInflater = (LayoutInflater) context
 93                     .getSystemService(inflater);
 94             try
 95             {
 96                 InputStream is = getNetInputStream(listUrl);
 97                 InputStreamReader isr = new InputStreamReader(is, "GBK");
 98                 BufferedReader br = new BufferedReader(isr);
 99                 String s = null;
100                 while ((s = br.readLine()) != null)
101                 {
102                     String[] data = s.split(",");
103                     if (data.length > 2)
104                     {
105                         ImageData imageData = new ImageData();
106                         imageData.url = data[0];
107                         Log.d("aa", imageData.url);
108                         imageData.applicationName = data[1];
109                         imageData.rating = Float.parseFloat(data[2]);
110                         imageDataList.add(imageData);
111                     }
112                 }
113                 is.close();
114             }
115             catch (Exception e)
116             {
117                 // TODO: handle exception
118             }
119         }
120 
121         @Override
122         public int getCount()
123         {
124             return imageDataList.size();
125         }
126 
127         @Override
128         public Object getItem(int position)
129         {
130             return position;
131         }
132 
133         @Override
134         public long getItemId(int position)
135         {
136             return position;
137         }
138 
139         @Override
140         public View getView(int position, View convertView, ViewGroup parent)
141         {
142             LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
143                     R.layout.item, null);
144             ImageView ivLogo = (ImageView) linearLayout
145                     .findViewById(R.id.ivLogo);
146 
147             TextView tvApplicationName = ((TextView) linearLayout
148                     .findViewById(R.id.tvApplicationName));
149 
150             TextView tvRating = (TextView) linearLayout
151                     .findViewById(R.id.tvRating);
152             RatingBar ratingBar = (RatingBar) linearLayout
153                     .findViewById(R.id.ratingbar);
154 
155             tvApplicationName
156                     .setText(imageDataList.get(position).applicationName);
157             tvRating
158                     .setText(String.valueOf(imageDataList.get(position).rating));
159             ratingBar.setRating(imageDataList.get(position).rating);
160 
161             try
162             {
163                 InputStream is = getNetInputStream(rootUrl
164                         + imageDataList.get(position).url);
165                 Bitmap bitmap = BitmapFactory.decodeStream(is);
166                 is.close();
167                 ivLogo.setImageBitmap(bitmap);
168             }
169             catch (Exception e)
170             {
171             }
172 
173             return linearLayout;
174         }
175     }
176 
177     @Override
178     public void onCreate(Bundle savedInstanceState)
179     {
180         super.onCreate(savedInstanceState);
181         ApkListAdapter apkListAdapter = new ApkListAdapter(this);
182         setListAdapter(apkListAdapter);
183 
184     }
185 }

要想实现最终的功能,手机和电脑必须在同一个内网上,手机建立热点,电脑连上。

最终效果:

源码传送门

在台式机上发现

http://219.245.64.92:8080/apk/ 本机IP

http://219.245.64.1:8080/apk/ 都能找到

哈哈,发现最终可能是师兄留下的,原来别人早已干过,惭愧!

posted @ 2015-04-01 23:15  疾风剑  阅读(449)  评论(0编辑  收藏  举报