public?class?MainActivity?extends?Activity?{
private?ImageView?imageView;
private?ListView?listView;
List<Bitmap>?list?=?GetBitmap.getbitmap();
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView?=?(ListView)?findViewById(R.id.listview);
new?Myasynctak().execute("mnt/sdcard");
}
class?Myasynctak?extends?AsyncTask<String,?Void,?List<ImgBen>>?{
@Override
protected?List<ImgBen>?doInBackground(String...?params)?{
//?TODO?Auto-generated?method?stub
return?getUrl(params[0]);
}
@Override
protected?void?onPostExecute(List<ImgBen>?result)?{
//?TODO?Auto-generated?method?stub
super.onPostExecute(result);
MyAdpter?adpter=new?MyAdpter(MainActivity.this,?result);
listView.setAdapter(adpter);
}
}
private?List<ImgBen>?getUrl(String?url)?{
List<ImgBen>?list?=?new?ArrayList<ImgBen>();
File?file?=?new?File(url);
//?判斷是否是文件
if?(file.exists())?{
File[]?arry?=?file.listFiles();
for?(int?i?=?0;?i?<?arry.length;?i++)?{
String?name?=?arry[i].getName();//?獲取這個(gè)文件的名稱(chēng)
if?(name.endsWith(".jpg")?||?name.endsWith(".png"))?{
//?獲取他的絕對(duì)路徑
String?filepath?=?arry[i].getAbsolutePath();
ImgBen?ben?=?new?ImgBen(filepath);
list.add(ben);
}
}
}
return?list;
}
}//這個(gè)Javabeanpublic class ImgBen {private String url;public ImgBen() { super(); // TODO Auto-generated constructor stub}public ImgBen(String url) { super(); this.url = url;}public String getUrl() { return url;}public void setUrl(String url) { this.url = url;}}//這個(gè)是加載圖片的異步操作public class LoginThread { ImageView mimageView; Handler mhandler=new Handler(){ public void handleMessage(android.os.Message msg) { mimageView.setImageBitmap((Bitmap) msg.obj); }; }; public void showThread(ImageView imageView, final String url) { mimageView=imageView; new Thread(){ @Override public void run() { // TODO Auto-generated method stub super.run(); Bitmap bitmap=getbitmapImage(url); Message message=new Message(); message.obj=bitmap; mhandler.sendMessage(message); } }.start(); } public Bitmap getbitmapImage(String url){ Bitmap bitmap=null; FileInputStream fis=null; BufferedInputStream bis=null; try { fis=new FileInputStream(url); bis=new BufferedInputStream(fis); bitmap=BitmapFactory.decodeStream(bis); return bitmap; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { bis.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; }}//這個(gè)是我適配器public class MyAdpter extends BaseAdapter { private LayoutInflater inflater; private List<ImgBen>list; public MyAdpter(Context context,List<ImgBen>list) { inflater = LayoutInflater.from(context); this.list=list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.item, null); ImgBen bitmap=list.get(position); ImageView imageView= (ImageView) view.findViewById(R.id.image); new LoginThread().showThread(imageView, bitmap.getUrl()); return view; }}
- 2 回答
- 0 關(guān)注
- 1697 瀏覽
添加回答
舉報(bào)
0/150
提交
取消