I'm trying to replace a deprecated part of code with kotlin. I need it to let me pick a folder, do a check to make sure it's the correct folder (a Morrowind.ini file and "Data Files" directory must be present) and then return that path to a variable game_files.
My android knowledge is extremely limited and I've been at this for weeks.
findPreference("game_files").setOnPreferenceClickListener {
// Use ACTION_OPEN_DOCUMENT_TREE intent to allow the user to choose a directory
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE)
true
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK) {
data?.data?.also { uri ->
// Persist access permissions for the selected directory using takePersistableUriPermission
activity.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
// Use the selected directory URI to perform operations
val selectedDirectory = DocumentFile.fromTreeUri(activity, uri)
val path = selectedDirectory?.uri?.path ?: ""
setupData(path)
}
}
}
which should send the data path to
private fun setupData(path: String) {
val sharedPref = preferenceScreen.sharedPreferences
// reset the setting so that it's erased on error instead of keeping
// possibly stale value
var gameFiles = ""
val inst = GameInstaller(path)
if (inst.check()) {
val iniFile = File(path, "Morrowind.ini")
val dataFilesFolder = File(path, "Data Files")
if (iniFile.exists() && dataFilesFolder.exists() && dataFilesFolder.isDirectory) {
inst.setNomedia()
if (!inst.convertIni(sharedPref.getString("pref_encoding", GameInstaller.DEFAULT_CHARSET_PREF)!!)) {
showError(R.string.data_error_title, R.string.ini_error_message)
} else {
gameFiles = path
}
} else {
// If either Morrowind.ini or Data Files folder is missing
showError(R.string.data_error_title, R.string.data_error_message)
I can't get it to work though. It lets me pick a folder and I can clearly see the file and directory there but receive the error message about them missing. I hope this is a better explanation and only relevant code. Thank you for taking the time to look at this.
I changed the first part to
and the second part is
Incase anyone is wondering about this