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();// 获取这个文件的名称 if (name.endsWith(".jpg") || name.endsWith(".png")) { // 获取他的绝对路径 String filepath = arry[i].getAbsolutePath(); ImgBen ben = new ImgBen(filepath); list.add(ben); } } } return list; } }
//这个Javabean
public 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;
}
}
//这个是加载图片的异步操作
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;
}
}
//这个是我适配器
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;
}
}
Hello橙子
ArryLuo
相关分类