Merge pull request #1977 from vector-im/feature/networkonmainthread

Avoid NetworkOnMainThreadException when setting a user avatar
This commit is contained in:
Benoit Marty 2020-08-21 16:48:19 +02:00 committed by GitHub
commit 52e66b09dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
- Renew turnserver credentials when ttl runs out
Translations 🗣:

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()