после рабочих дней я сделал рабочий код
для основного макета, который я использовал:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
другой макет для отображения изображений в режиме просмотра изображений:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/imageV"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@mipmap/ic_launcher" />
вот основное занятие:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//getting folder path
var gpath: String = Environment.getExternalStorageDirectory().absolutePath
var spath = "specificFolderName"
var path = File(gpath + File.separator + spath)
//getting all images from that path
var list = imageReader(path)
//get gridview from resources
var gv = findViewById<GridView>(R.id.gridview)
//set gridview adapter from ImageA class
gv.adapter = ImageA(this, list)
}
fun imageReader(root: File): ArrayList<File> {
val fileList: ArrayList<File> = ArrayList()
val listAllFiles = root.listFiles()
if (listAllFiles != null && listAllFiles.size > 0) {
for (currentFile in listAllFiles) {
if (currentFile.name.endsWith(".jpg")) {
fileList.add(currentFile.absoluteFile)
}
}
}
return fileList
}
}
вот класс адаптера:
class ImageA (c: Context, list: ArrayList<File>) : BaseAdapter() {
var list:ArrayList<File> = list
private var mcontext: Context? = c
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var convertV = LayoutInflater.from(mcontext).inflate(R.layout.single_grid,
parent, false)
var iv = convertV.findViewById<ImageView>(R.id.imageV)
var bitmap = MediaStore.Images.Media.getBitmap(mcontext?.contentResolver,
Uri.fromFile(list[position]))
iv.setImageBitmap(bitmap)
return convertV
}
override fun getItem(position: Int): Any {
return list[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return list.size
}
}