diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt index 1b0ccbb489..b988f2253c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt @@ -109,18 +109,23 @@ internal class FileUploader @Inject constructor( filename: String?, mimeType: String?, progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse { - val inputStream = withContext(Dispatchers.IO) { - context.contentResolver.openInputStream(uri) - } ?: throw FileNotFoundException() - val workingFile = temporaryFileCreator.create() - workingFile.outputStream().use { - inputStream.copyTo(it) - } + val workingFile = context.copyUriToTempFile(uri) return uploadFile(workingFile, filename, mimeType, progressListener).also { tryOrNull { workingFile.delete() } } } + private suspend fun Context.copyUriToTempFile(uri: Uri): File { + return withContext(Dispatchers.IO) { + val inputStream = contentResolver.openInputStream(uri) ?: throw FileNotFoundException() + val workingFile = temporaryFileCreator.create() + workingFile.outputStream().use { + inputStream.copyTo(it) + } + workingFile + } + } + private suspend fun upload(uploadBody: RequestBody, filename: String?, progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {