1.获取某个目录下图片路径list
/** * Get pictures under directory of strPath * @param strPath * @return list */ public List<String> getPictures(final String strPath) { List<String> list = new ArrayList<String>(); File file = new File(strPath); File[] files = file.listFiles(); if (files == null) { return null; } for(int i = 0; i < files.length; i++) { final File f = files[i]; if(f.isFile()) { try{ int idx = f.getPath().lastIndexOf("."); if (idx <= 0) { continue; } String suffix = f.getPath().substring(idx); if (suffix.toLowerCase().equals(".jpg") || suffix.toLowerCase().equals(".jpeg") || suffix.toLowerCase().equals(".bmp") || suffix.toLowerCase().equals(".png") || suffix.toLowerCase().equals(".gif") ) { list.add(f.getPath()); } } catch(Exception e) { e.printStackTrace(); } } } return list; }
2.调用上述方法获取sd卡下的图片并显示
List<String> list = getPictures(Environment.getExternalStorageDirectory() + ""); if (list != null) { Log.d(TAG, "list.size = " + list.size()); for (int i = 0; i < list.size(); i++) { Bitmap bm = BitmapFactory.decodeFile(list.get(i)); int top = 30; if (i > 0) { top += BitmapFactory.decodeFile(list.get(i - 1)).getHeight() + 2; } canvas.drawBitmap(bm, 0, top, paint); } } else { Log.d(TAG, "list is null!!!")