Unverified Commit de7eac9a authored by Lucio Maciel's avatar Lucio Maciel Committed by GitHub

Merge pull request #1639 from RocketChat/fix/out-of-memory

[FIX] Out of memory when sending large image
parents 4df382a0 d4c2285d
package chat.rocket.android.chatroom.presentation package chat.rocket.android.chatroom.presentation
import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.analytics.AnalyticsManager import chat.rocket.android.analytics.AnalyticsManager
...@@ -80,6 +81,7 @@ import org.threeten.bp.Instant ...@@ -80,6 +81,7 @@ import org.threeten.bp.Instant
import timber.log.Timber import timber.log.Timber
import java.io.InputStream import java.io.InputStream
import java.util.* import java.util.*
import java.util.zip.DeflaterInputStream
import javax.inject.Inject import javax.inject.Inject
class ChatRoomPresenter @Inject constructor( class ChatRoomPresenter @Inject constructor(
...@@ -331,7 +333,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -331,7 +333,7 @@ class ChatRoomPresenter @Inject constructor(
view.showFileSelection(settings.uploadMimeTypeFilter()) view.showFileSelection(settings.uploadMimeTypeFilter())
} }
fun uploadFile(roomId: String, uri: Uri, msg: String) { fun uploadFile(roomId: String, uri: Uri, msg: String, bitmap: Bitmap? = null) {
launchUI(strategy) { launchUI(strategy) {
view.showLoading() view.showLoading()
try { try {
...@@ -348,12 +350,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -348,12 +350,8 @@ class ChatRoomPresenter @Inject constructor(
else -> { else -> {
var inputStream: InputStream? = uriInteractor.getInputStream(uri) var inputStream: InputStream? = uriInteractor.getInputStream(uri)
if (mimeType.contains("image")) { bitmap?.compressImageAndGetInputStream(mimeType)?.let {
uriInteractor.getBitmap(uri)?.let { inputStream = it
it.compressImageAndGetInputStream(mimeType)?.let {
inputStream = it
}
}
} }
retryIO("uploadFile($roomId, $fileName, $mimeType") { retryIO("uploadFile($roomId, $fileName, $mimeType") {
......
package chat.rocket.android.chatroom.ui package chat.rocket.android.chatroom.ui
import android.graphics.Bitmap
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import androidx.core.view.isVisible import androidx.core.view.isVisible
import chat.rocket.android.emoji.internal.GlideApp
import chat.rocket.android.util.extensions.getFileName import chat.rocket.android.util.extensions.getFileName
import chat.rocket.android.util.extensions.getMimeType import chat.rocket.android.util.extensions.getMimeType
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) { fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
activity?.let { fragmentActivity -> var bitmap: Bitmap? = null
uri.getMimeType(fragmentActivity).let { mimeType ->
activity?.let { context ->
uri.getMimeType(context).let { mimeType ->
description.text.clear() description.text.clear()
when { when {
mimeType.startsWith("image") -> { mimeType.startsWith("image") -> {
imagePreview.isVisible = true GlideApp
imagePreview.setImageURI(uri) .with(context)
} .asBitmap()
mimeType.startsWith("video") -> { .load(uri)
audioVideoAttachment.isVisible = true .override(500, 500)
.centerCrop()
.into(object : SimpleTarget<Bitmap>() {
override fun onResourceReady(
resource: Bitmap,
transition: Transition<in Bitmap>?
) {
bitmap = resource
imagePreview.isVisible = true
imagePreview.setImageBitmap(resource)
}
})
} }
mimeType.startsWith("video") -> audioVideoAttachment.isVisible = true
else -> { else -> {
textFile.isVisible = true textFile.isVisible = true
textFile.text = uri.getFileName(fragmentActivity) textFile.text = uri.getFileName(context)
} }
} }
} }
} }
sendButton.setOnClickListener { sendButton.setOnClickListener {
presenter.uploadFile(chatRoomId, uri, (citation ?: "") + description.text.toString()) presenter.uploadFile(
chatRoomId,
uri,
(citation ?: "") + description.text.toString(),
bitmap
)
alertDialog.dismiss() alertDialog.dismiss()
} }
cancelButton.setOnClickListener { alertDialog.dismiss() } cancelButton.setOnClickListener { alertDialog.dismiss() }
......
...@@ -10,15 +10,19 @@ import android.provider.DocumentsContract ...@@ -10,15 +10,19 @@ import android.provider.DocumentsContract
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.OpenableColumns import android.provider.OpenableColumns
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
import java.io.* import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
fun Uri.getFileName(context: Context): String? { fun Uri.getFileName(context: Context): String? {
val cursor = context.contentResolver.query(this, null, null, null, null, null) val cursor = context.contentResolver.query(this, null, null, null, null, null)
var fileName: String? = null var fileName: String? = null
cursor.use { cursor -> cursor?.use {
if (cursor != null && cursor.moveToFirst()) { if (it.moveToFirst()) {
fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)) fileName = it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
} }
} }
return fileName return fileName
...@@ -29,7 +33,7 @@ fun Uri.getFileSize(context: Context): Int { ...@@ -29,7 +33,7 @@ fun Uri.getFileSize(context: Context): Int {
if (scheme == ContentResolver.SCHEME_CONTENT) { if (scheme == ContentResolver.SCHEME_CONTENT) {
try { try {
val fileInputStream = context.contentResolver.openInputStream(this) val fileInputStream = context.contentResolver.openInputStream(this)
fileSize = fileInputStream.available().toString() fileSize = fileInputStream?.available().toString()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
...@@ -67,14 +71,17 @@ fun Uri.isVirtualFile(context: Context): Boolean { ...@@ -67,14 +71,17 @@ fun Uri.isVirtualFile(context: Context): Boolean {
val cursor = context.contentResolver.query( val cursor = context.contentResolver.query(
this, this,
arrayOf(DocumentsContract.Document.COLUMN_FLAGS), arrayOf(DocumentsContract.Document.COLUMN_FLAGS),
null, null, null null,
null,
null
) )
var flags = 0 var flags = 0
if (cursor.moveToFirst()) { cursor?.use {
flags = cursor.getInt(0) if (it.moveToFirst()) {
flags = it.getInt(0)
}
} }
cursor.close()
return flags and DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT != 0 return flags and DocumentsContract.Document.FLAG_VIRTUAL_DOCUMENT != 0
} }
...@@ -104,4 +111,4 @@ fun Uri.getInputStream(context: Context): InputStream? { ...@@ -104,4 +111,4 @@ fun Uri.getInputStream(context: Context): InputStream? {
fun Uri.getBitmpap(context: Context): Bitmap? { fun Uri.getBitmpap(context: Context): Bitmap? {
return MediaStore.Images.Media.getBitmap(context.contentResolver, this) return MediaStore.Images.Media.getBitmap(context.contentResolver, this)
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment