mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-15 01:35:07 +08:00
Merge pull request #4783 from vector-im/feature/adm/unable-to-change-avatar
Unable to change avatar due to `NetworkOnMainThread`
This commit is contained in:
commit
5407c84b2f
1
changelog.d/4767.bugfix
Normal file
1
changelog.d/4767.bugfix
Normal file
@ -0,0 +1 @@
|
||||
Fixing unable to change change avatar in some scenarios
|
@ -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 {
|
||||
|
@ -68,7 +68,7 @@ internal class DefaultProfileService @Inject constructor(private val taskExecuto
|
||||
}
|
||||
|
||||
override suspend fun updateAvatar(userId: String, newAvatarUri: Uri, fileName: String) {
|
||||
withContext(coroutineDispatchers.main) {
|
||||
withContext(coroutineDispatchers.io) {
|
||||
val response = fileUploader.uploadFromUri(newAvatarUri, fileName, MimeTypes.Jpeg)
|
||||
setAvatarUrlTask.execute(SetAvatarUrlTask.Params(userId = userId, newAvatarUrl = response.contentUri))
|
||||
userStore.updateAvatar(userId, response.contentUri)
|
||||
|
Loading…
Reference in New Issue
Block a user