版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiyangyang8110/article/details/83989712
sd卡上的图片文件夹名称是Pictures
获取sd卡根路径下的api
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Pictures"+"/"+"01.jpg";//获取视频路径
1先加读取sd卡权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2代码
private List<String> mListString =getImagePathFromSD();
private ImageSwitcher mImagSwitcher ;
mImagSwitcher = (ImageSwitcher) findViewById(R.id.imagV_ad);
if(mImagSwitcher!=null){
mImagSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// makeView返回的是当前需要显示的ImageView控件,用于填充进ImageSwitcher中
return new ImageView(SpeechMainActivity.this);
}
});
}
if(mImagSwitcher!=null){
mImagSwitcher.postDelayed(new Runnable() {
int currentIndex= 0;
@Override
public void run() {
// mImagSwitcher.setImageResource(images[currentIndex]);
Log.d(TAG, "mListString:"+mListString.get(currentIndex));
Bitmap bit = BitmapFactory.decodeFile(mListString.get(currentIndex));
Drawable drawable = new BitmapDrawable(bit);
mImagSwitcher.setBackgroundDrawable(drawable);
if(currentIndex ==(mListString.size() - 1))
currentIndex = 0;
else
currentIndex++;
mImagSwitcher.postDelayed(this,1000);
}
},200);
}
/**
* 从sd卡获取图片资源
* @return
*/
private List<String> getImagePathFromSD() {
// 图片列表
List<String> imagePathList = new ArrayList<String>();
// 得到sd卡内image文件夹的路径 File.separator(/)
// String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+"Pictures"+"/"+"01.jpg";//获取视频路径
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"Pictures";
// 得到该路径文件夹下所有的文件
File fileAll = new File(filePath);
File[] files = fileAll.listFiles();
// 将所有的文件存入ArrayList中,并过滤所有图片格式的文件
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (checkIsImageFile(file.getPath())) {
imagePathList.add(file.getPath());
}
}
// 返回得到的图片列表
return imagePathList;
}
/**
* 检查扩展名,得到图片格式的文件
* @param fName 文件名
* @return
*/
@SuppressLint("DefaultLocale")
private boolean checkIsImageFile(String fName) {
boolean isImageFile = false;
// 获取扩展名
String FileEnd = fName.substring(fName.lastIndexOf(".") + 1,
fName.length()).toLowerCase();
if (FileEnd.equals("jpg") || FileEnd.equals("png") || FileEnd.equals("gif")
|| FileEnd.equals("jpeg")|| FileEnd.equals("bmp") ) {
isImageFile = true;
} else {
isImageFile = false;
}
return isImageFile;
}