在gridview中显示文件夹内容 [英] show folder content in gridview
本文介绍了在gridview中显示文件夹内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我在外部存储中有一个特定的文件夹,我想将该文件夹中的所有.jpg图片显示到gridview中,我正在关注
So I have a specific folder in external storage, and I want to show all .jpg pictures from that folder into a gridview, i'm following this tutorial and it's fine but it gets pictures from res/drawable when i need to get them from sd-card, I read all examples and tutorials about this problem but most of it are toooooo old and inapplicable for kotlin, any help is appreciated
推荐答案
工作几天后,我编写了工作代码
after days of working I made a working code
我使用的主要布局:
<?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"
/>
在图像视图中显示图像的另一种布局:
another layout to display images in image view :
<?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
}
}
这篇关于在gridview中显示文件夹内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文