我正在使用此代码从特定文件夹中加载网格视图中的图像:
public class GalleryClass extends AsyncTask<Void,Void,Void> {
public static ArrayList<String> f = new ArrayList<>();
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
GalleryFrag.gridView.setAdapter(new ImageAdapter(GalleryFrag.context));
}
@Override
protected Void doInBackground(Void... voids) {
File dir = new File(String.valueOf(Environment.getExternalStoragePublicDirectory("MyFolder")));
File[] FileList = dir.listFiles();
if (FileList !=null) {
for (File aFileList : FileList) {
f.add(aFileList.getAbsolutePath());
}
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
f.clear();
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ImageAdapter(Context context1) {
mContext = context1;
}
@Override
public int getCount() {
return f.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(230, 230));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
Glide.with(GalleryFrag.context).load("file://" + f.get(position))
.apply(new RequestOptions()
.placeholder(R.drawable.ic_photo)
.error(R.drawable.ic_error))
.into(imageView);
return imageView;
}
}
}
这是Gallery.xml代码:
<?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"
android:background="@color/cardcolor"
tools:context=".GalleryFrag">
<!-- TODO: Update blank fragment layout -->
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:stretchMode="columnWidth"/>
</RelativeLayout>
当没有子文件夹时,我的文件夹中的所有内容都正常显示,其他是showng ic_error
占位符。
但是我需要在主文件夹MyFolder
中添加子文件夹,当点击文件夹时,它的所有图像也应该显示在GridView
中。