Merge pull request #1977 from vector-im/feature/networkonmainthread
Avoid NetworkOnMainThreadException when setting a user avatar
This commit is contained in:
commit
52e66b09dc
@ -25,6 +25,7 @@ Bugfix 🐛:
|
|||||||
- Failed to build unique file (#1954)
|
- Failed to build unique file (#1954)
|
||||||
- Highlighted Event when opening a permalink from another room (#1033)
|
- Highlighted Event when opening a permalink from another room (#1033)
|
||||||
- A Kick appears has "someone has made no change" (#1959)
|
- A Kick appears has "someone has made no change" (#1959)
|
||||||
|
- Avoid NetworkOnMainThreadException when setting a user avatar
|
||||||
- Renew turnserver credentials when ttl runs out
|
- Renew turnserver credentials when ttl runs out
|
||||||
|
|
||||||
Translations 🗣:
|
Translations 🗣:
|
||||||
|
@ -20,11 +20,6 @@ package org.matrix.android.sdk.internal.session.content
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import com.squareup.moshi.Moshi
|
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.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||||
@ -35,6 +30,11 @@ import okhttp3.RequestBody
|
|||||||
import okhttp3.RequestBody.Companion.asRequestBody
|
import okhttp3.RequestBody.Companion.asRequestBody
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import org.greenrobot.eventbus.EventBus
|
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.File
|
||||||
import java.io.FileNotFoundException
|
import java.io.FileNotFoundException
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
@ -70,15 +70,14 @@ internal class FileUploader @Inject constructor(@Authenticated
|
|||||||
filename: String?,
|
filename: String?,
|
||||||
mimeType: String?,
|
mimeType: String?,
|
||||||
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse {
|
||||||
val inputStream = withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
context.contentResolver.openInputStream(uri)
|
val inputStream = context.contentResolver.openInputStream(uri) ?: throw FileNotFoundException()
|
||||||
} ?: throw FileNotFoundException()
|
|
||||||
|
|
||||||
inputStream.use {
|
inputStream.use {
|
||||||
return uploadByteArray(it.readBytes(), filename, mimeType, progressListener)
|
uploadByteArray(it.readBytes(), filename, mimeType, progressListener)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun upload(uploadBody: RequestBody, filename: String?, progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {
|
private suspend fun upload(uploadBody: RequestBody, filename: String?, progressListener: ProgressRequestBody.Listener?): ContentUploadResponse {
|
||||||
val urlBuilder = uploadUrl.toHttpUrlOrNull()?.newBuilder() ?: throw RuntimeException()
|
val urlBuilder = uploadUrl.toHttpUrlOrNull()?.newBuilder() ?: throw RuntimeException()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user