Android展示指定文件夹下面的所有的图片

时间:2020-12-29 09:56:46

先上效果图

Android展示指定文件夹下面的所有的图片

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xiaoge.openfiles.MainActivity">


<android.support.v4.view.ViewPager
android:id="@+id/pics"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>


<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:textColor="@android:color/black"
android:textSize="16sp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="共0张照片,左右滑动切换下一张"/>


</RelativeLayout>

activity中的代码

public class MainActivity extends AppCompatActivity {

/** 展示内容的ViewPager **/
private ViewPager pics;
private Context mContext;
/** 存放当前文件夹下所有文件的路径的集合 **/
private static ArrayList<String> paths = new ArrayList<String>();
/** 展示当前文件夹下面的图片的总数 **/
private TextView count;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initView();
}

private void initView() {
pics = (ViewPager) findViewById(R.id.pics);
count = (TextView) findViewById(R.id.count);

Map<String,Bitmap> maps = new TreeMap<String, Bitmap>();
try {
maps = buildThum();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
/** 存放当前文件夹下的图片转换成的bitmap **/
final List<Bitmap> datas = new ArrayList<>();
/** 存放所有的ImageView的集合 **/
final List<ImageView> images = new ArrayList<>();

for (Map.Entry<String,Bitmap> entry : maps.entrySet()) {
datas.add(entry.getValue());
ImageView iv = new ImageView(mContext);
iv.setImageBitmap(entry.getValue());
Glide.with(mContext)
.load(entry.getKey())
.into(iv);
images.add(iv);
}

count.setText( (datas.size() == 0 ? 0 : 1) + " / "+datas.size()+" 张");

// 为viewPager设置适配器
pics.setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return datas.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(images.get(position), 0);//添加页卡
return images.get(position);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(images.get(position));
}
});

// viewPager 设置页面改变的监听
pics.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
count.setText((position + 1) +" / "+datas.size()+" 张");
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}

/**
* 判断当前存储卡是否可用
**/

public boolean checkSDCardAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

/**
* 获取当前需要查询的文件夹
**/

public String takePicRootDir(Context context) {
if (checkSDCardAvailable()) {
return Environment.getExternalStorageDirectory() + File.separator + "runman";
} else {
return context.getFilesDir().getAbsolutePath() + File.separator + "runman";
}
}

/**
* 描述: 获取指定文件夹下面的所有文件目录
* 作者: gyz
* 时间: 2017-01-17 17:02:40
*/

private Map<String, Bitmap> buildThum() throws FileNotFoundException {
File baseFile = new File(takePicRootDir(mContext));
// 使用TreeMap,排序问题就不需要纠结了
Map<String, Bitmap> maps = new TreeMap<String, Bitmap>();
if (baseFile != null && baseFile.exists()) {
paths = imagePath(baseFile);

if (!paths.isEmpty()) {
for (int i = 0; i < paths.size(); i++) {
Bitmap bitmap = BitmapFactory.decodeFile(paths.get(i));

maps.put(paths.get(i), bitmap);
}
}
}

return maps;
}

/**
* 描述: 获取图片列表
* 作者: gyz
* 时间: ${DATE} ${TIME}
*/

private static ArrayList<String> imagePath(File file) {
ArrayList<String> list = new ArrayList<String>();

File[] files = file.listFiles();
for (File f : files) {
list.add(f.getAbsolutePath());
}
Collections.sort(list);
return list;
}
}

完整代码:

DEMO下载