Android + Kotlin: Find Folders And Store Their Names As String Array
Solution 1:
In Android you should strictly specify the path of the folder you want to view.
Start point could be Environment.getExternalStorageDirectory()
- the primary shared/external storage directory.
Then you could get list of files with method dir.listFiles()
. If you need to filter it somehow (for example have only directories) you could add a FileFilter: dir.listFiles(FileFilter { it.isDirectory })
Another important thing is that you have to have a permission for reading external storange. Thus you should add to your Manifest.xml:
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/>
Starting from Android 6.0 you should additionally ask for this permission:
Fragment.requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), READ_STORAGE_RC)
And handle results in Fragment.onRequestPermissionsResult method.
I created example of how it could be used together. You need to create activity and add a Fragment inside: DirListFragment
classDirListFragment : Fragment() {
privateval adapter by lazy { DirListAdapter { dir = it } }
privatevar dir: File by observable(Environment.getExternalStorageDirectory()) { _, _, _ -> onNewParent() }
overridefunonCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(R.layout.fragment_dir_list, container, false)
overridefunonViewCreated(view: View, savedInstanceState: Bundle?) {
checkPermissionReadStorage(requireActivity())
list.adapter = adapter
up.setOnClickListener { dir = dir.parentFile }
onNewParent()
}
privatefunonNewParent() {
parent.text = dir.absolutePath
adapter.items = dir.listFiles(FileFilter { it.isDirectory })?.toList() ?: emptyList()
}
overridefunonRequestPermissionsResult(requestCode: Int, permissions: Array<outString>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == READ_STORAGE_RC && permissionGranted(requireActivity())) onNewParent()
}
privatefuncheckPermissionReadStorage(activity: Activity) {
if (!permissionGranted(activity)) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), READ_STORAGE_RC)
}
}
privatefunpermissionGranted(activity: Activity) =
ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
privatecompanionobject {
constval READ_STORAGE_RC = 123
}
}
DirListAdapter
classDirListAdapter(privateval clickListener: (file: File) -> Unit) :
RecyclerView.Adapter<DirListAdapter.ViewHolder>() {
var items by Delegates.observable(listOf<File>()) { _, _, _ -> notifyDataSetChanged() }
overridefunonCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(parent)
overridefungetItemCount() = items.size
overridefunonBindViewHolder(holder: ViewHolder, position: Int) {
holder.caption.text = items[position].name
holder.caption.setOnClickListener { clickListener(items[position]) }
}
classViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder
(LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)) {
val caption: TextView = itemView.findViewById(android.R.id.text1)
}
}
fragment_dir_list.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:padding="8dp"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/up"android:text="UP"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/parent"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/up"android:layout_marginBottom="8dp"android:singleLine="true"android:ellipsize="start"tools:text="Parent dir"/><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/list"android:layout_below="@id/parent"android:layout_width="match_parent"android:layout_height="match_parent"app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/></RelativeLayout>
Hope this will help to understand the basis.
Post a Comment for "Android + Kotlin: Find Folders And Store Their Names As String Array"