这是读取SD卡的程序:
public class SDcardImageService { 
public static List<String> getImagesFromSD() {
    List<String> imagePaths = new ArrayList<String>();
    if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)
                    || Environment.getExternalStorageState().equals(
                                    Environment.MEDIA_MOUNTED_READ_ONLY)) {
            File file = new File("/mnt/sdcard/image");
//            File file = Environment.getExternalStorageDirectory()
            //这里只做了单级目录,多级目录做一个简单的判断递归就ok了
            if(file !=null){
                    File[] files = file.listFiles();//列出所有文件
                    for (File f : files) 
                    { 
                            if(isImageFile(f.getPath())){
                                    imagePaths.add(f.getPath());
                            }
                    }  
            }   
           
    }
    return imagePaths;   
}private static String[] imageFormatSet = new String[]{"jpg","png","gif"};
private static boolean isImageFile(String path) {
    
    for(String format : imageFormatSet){
            if(path.contains(format)){
                    return true;
            }
    }
    return false;
}
}绑定到GridView的Adapter的getView()方法:public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(OrderActivity.this);
View v = null;
ImageView imageView = null;
TextView nametv = null;
if(convertView == null){
v = inflater.inflate(R.layout.diancai_view, null);
v.setPadding(8, 8, 8, 8);
}else{
v = (View)convertView;
}
imageView = (ImageView) v.findViewById(R.id.diancai_view_ImageView01);
nametv = (TextView) v.findViewById(R.id.diancai_view_nametv01);
//这里给ImageView设置显示的图片
imageView.setImageURI(Uri.parse(mImagePath.get(position))); 

CheckMenu cm = (CheckMenu) list.get(position); 
nametv.setText(cm.getName());
return v;
}我要怎样改一下程序呢?

解决方案 »

  1.   


     File file = new File("/mnt/sdcard/menuimage");
                if(file !=null){
                        File[] files = file.listFiles();                    
                        for (File f : files)                     { 
                                if(isImageFile(f.getPath())){
                                    imagePaths.add(f.getPath());
                                }
                        }
                        Collections.sort(imagePaths,String.CASE_INSENSITIVE_ORDER); //排序            }