在gridview中显示文件夹内容

时间:2018-07-01 12:42:26

标签: android gridview kotlin

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

2 个答案:

答案 0 :(得分:1)

根据Here所述,您可以像这样以字节数组形式读取文件

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可以将其转换为这样的位图

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

希望这对您有所帮助。

答案 1 :(得分:0)

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

我使用的主要布局:

<?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
}

}