Avoid NetworkOnMainThreadException when setting a user avatar

This commit is contained in:
Benoit Marty 2020-08-21 16:11:22 +02:00
parent 35dbab2ff5
commit a8c6678efc
2 changed files with 11 additions and 11 deletions

View file

@ -25,6 +25,7 @@ Bugfix 🐛:
- Failed to build unique file (#1954)
- Highlighted Event when opening a permalink from another room (#1033)
- A Kick appears has "someone has made no change" (#1959)
- Avoid NetworkOnMainThreadException when setting a user avatar
Translations 🗣:
- Add PlayStore description resources in the Triple-T format, to let Weblate handle them

View file

@ -20,11 +20,6 @@ package org.matrix.android.sdk.internal.session.content
import android.content.Context
import android.net.Uri
import com.squareup.moshi.Moshi
import org.matrix.android.sdk.api.session.content.ContentUrlResolver
import org.matrix.android.sdk.internal.di.Authenticated
import org.matrix.android.sdk.internal.network.ProgressRequestBody
import org.matrix.android.sdk.internal.network.awaitResponse
import org.matrix.android.sdk.internal.network.toFailure
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
@ -35,6 +30,11 @@ import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.greenrobot.eventbus.EventBus
import org.matrix.android.sdk.api.session.content.ContentUrlResolver
import org.matrix.android.sdk.internal.di.Authenticated
import org.matrix.android.sdk.internal.network.ProgressRequestBody
import org.matrix.android.sdk.internal.network.awaitResponse
import org.matrix.android.sdk.internal.network.toFailure
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
@ -70,15 +70,14 @@ internal class FileUploader @Inject constructor(@Authenticated
filename: String?,
mimeType: String?,
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
val inputStream = withContext(Dispatchers.IO) {
context.contentResolver.openInputStream(uri)
} ?: throw FileNotFoundException()
return withContext(Dispatchers.IO) {
val inputStream = context.contentResolver.openInputStream(uri) ?: throw FileNotFoundException()
inputStream.use {
return uploadByteArray(it.readBytes(), filename, mimeType, progressListener)
inputStream.use {
uploadByteArray(it.readBytes(), filename, mimeType, progressListener)
}
}
}
private suspend fun upload(uploadBody: RequestBody, filename: String?, progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {
val urlBuilder = uploadUrl.toHttpUrlOrNull()?.newBuilder() ?: throw RuntimeException()