参考文章:http://www.2cto.com/kf/201502/376967.html
xml文件
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xingyue.xingyue.PassiveLearningActivity"
android:background="@color/white">
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipperPhoto);
//获取制定文件夹下的图片
listPhoto = CommonalityMethod.getListOfPictures(CommonalityMethod.makeRootDirectory(FilePath.photoPath));}
}
//Photos 类,存放photo名字
public class Photos {
private String photoName;
public String getPhotoName() {
return photoName;
}
public void setPhotoName(String photoName) {
this.photoName = photoName;
}
}
//图片所在的文件路径
public class FilePath {
/*本地文件路径*/
public final static String localFolderPath = "/storage/sdcard0/jingjing";
/*被动学习图片路径*/
public final static String photoPath = "/storage/sdcard0/jingjing/photos";
}
public class CommonalityMethod {
private static String Tag = "CommonalityMethod";
//文件夹不存在则 生成文件夹
public static File makeRootDirectory(String filePath) {
File file = null;
try {
file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
} catch (Exception e) {
LogUtils.i(Tag, e+"");
}
return file;
}
//获取指定文件夹下的图片
public static List
File[] files = fileName.listFiles();// 列出所有文件
List
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
//筛选png类型的图片
if (file.getName().substring(file.getName().lastIndexOf(".")+1).equals("png")) {
Photos photos = new Photos();
photos.setPhotoName(file.getName());
listPhoto.add(photos);
}
}
}
return listPhoto;
}
}