show folder content in gridview

2019-08-06 13:24发布

问题:

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

回答1:

As documented Here you can read the file in a byte array form like this

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())
    }
}

Then using the BitmapFactory class API you can convert it to bitmap like this

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

Hope this helps you.



回答2:

after days of working I made a working code

for main layout i used :

<?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" />

here is the main activity :

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

here is adapter class :

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
}

}