关于android:在gridview中显示文件夹内容

show folder content in gridview

因此我在外部存储中有一个特定的文件夹,并且我想将该文件夹中的所有.jpg图片显示到gridview中,我正在按照本教程进行操作,这很好,但是当我需要时它可以从res / drawable获取图片从sd卡中获取它们,我阅读了有关此问题的所有示例和教程,但其中大多数都太旧了,不适用于kotlin,对您有所帮助


如本文所述,您可以像这样以字节数组形式读取文件

1
2
3
4
5
6
7
fun main(args: Array<String>) {
    val file = File("input"+File.separator+"image.jpg")
    var bytes:ByteArray = file.readBytes()
    for(byte in bytes){
        print(byte.toChar())
    }
}

然后使用BitmapFactory类API可以将其转换为这样的位图

1
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

希望这对您有所帮助。


工作几天后,我编写了工作代码

我使用的主要布局:

1
2
3
4
5
6
7
8
9
10
11
12
<?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"
/>

在图像视图中显示图像的另一种布局:

1
2
3
4
5
6
7
8
9
10
11
12
<?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" />

这是主要活动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}
}

这是适配器类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
}

}