Fix filePicker - Android
This commit is contained in:
parent
e32bda0f2c
commit
6e81547d83
2 changed files with 19 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package mg.dot.feufaro
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import feufaro.composeapp.generated.resources.Res
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
|
@ -30,6 +31,12 @@ class AndroidFileRepository(private val context: Context) : FileRepository {
|
|||
override suspend fun readFileLines(filePath: String): List<String> = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
when {
|
||||
filePath.startsWith("content://") -> {
|
||||
val uri = Uri.parse(filePath)
|
||||
val inputStream = context.contentResolver.openInputStream(uri)
|
||||
?: throw IOException("Impossible d'ouvrir : $filePath")
|
||||
inputStream.bufferedReader(Charsets.UTF_8).readLines()
|
||||
}
|
||||
filePath.startsWith("assets://") -> {
|
||||
readAssetFileLines(filePath)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,8 @@ actual fun launchFilePicker(
|
|||
if (mimeTypes.size > 1) {
|
||||
putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
|
||||
}
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
|
||||
}
|
||||
|
||||
// 3. Lance le sélecteur
|
||||
|
|
@ -50,6 +52,16 @@ fun setFilePickerActivity(activity: ComponentActivity) {
|
|||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val uri: Uri? = result.data?.data
|
||||
if (uri != null) {
|
||||
try {
|
||||
activity.contentResolver.takePersistableUriPermission(
|
||||
uri,
|
||||
Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
println("takePersistableUriPermission failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
fileSelectionCallback?.invoke(uri?.toString())
|
||||
} else {
|
||||
fileSelectionCallback?.invoke(null)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue