Skip to content

Creating a PlatformFile with Folder (Uri) + filename crashes #415

@MFlisar

Description

@MFlisar

Context: android

I do offer automatic backups in my app. So the user selects a folder and I do create files inside the folder like approx. following:

val backupFolder = PlatformFile.fromBookmarkData(backupFolderData)
val backupFileName = "backup.zip"
val backupFile = backupFolder / backupFileName // <= crashes

This leads to following crash:

io.github.vinceglb.filekit.exceptions.FileKitUriPathNotSupportedException: Uri-based PlatformFile does not have a Path representation
at io.github.vinceglb.filekit.PlatformFile_androidKt.toKotlinxIoPath(PlatformFile.android.kt:58)
at io.github.vinceglb.filekit.PlatformFile_nonWebKt.PlatformFile(PlatformFile.nonWeb.kt:21)
at io.github.vinceglb.filekit.PlatformFile_nonWebKt.div(PlatformFile.nonWeb.kt:102)

In general, android does support creating new uris with a folder uri + file name. Here's how a working android implementation could look like:

val folder: PlatformFile = ...
val fileName: String = ...
val subFile = when (val af = folder.androidFile) {
	is AndroidFile.FileWrapper -> folder / fileName
	is AndroidFile.UriWrapper -> {
		val extension = fileName.substringAfterLast('.', "")
		val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.lowercase()) ?: "application/octet-stream" // some default, don't know if this is the best option...
		val folder = DocumentFile.fromTreeUri(context, af.uri)
		val newFile = folder.createFile(mimeType, fileName)
		PlatformFile(AndroidFile.UriWrapper(newFile.getUri()))
	}
}

Could you add this support?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions