80,349
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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;
}
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); //排序
}