Open
Description
MY VIEWMODEL CODE (android app working fine):
private suspend fun readFileBytes(files: PlatformFile?, onSuccess: (ByteArray?) -> Unit) {
withContext(Dispatchers.IO) {
if(files!=null) {
val f = files.readBytes()
onSuccess(f)
} else {
onSuccess(null)
}
}
}
YOUR METHOD:
@OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
public actual suspend fun readBytes(): ByteArray = withContext(Dispatchers.IO) {
memScoped {
// Start accessing the security scoped resource
nsUrl.startAccessingSecurityScopedResource()
// Read the data
val error: CPointer<ObjCObjectVar<NSError?>> = alloc<ObjCObjectVar<NSError?>>().ptr
val nsData = NSData.dataWithContentsOfURL(nsUrl, NSDataReadingUncached, error)
?: throw IllegalStateException("Failed to read data from $nsUrl. Error: ${error.pointed.value}")
// Stop accessing the security scoped resource
nsUrl.stopAccessingSecurityScopedResource()
// Copy the data to a ByteArray
ByteArray(nsData.length.toInt()).apply {
memcpy(this.refTo(0), nsData.bytes, nsData.length)
}
}
}
Metadata
Assignees
Labels
No labels