Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
a9985b28
Unverified
Commit
a9985b28
authored
Oct 31, 2018
by
Lucio Maciel
Committed by
GitHub
Oct 31, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1810 from RocketChat/improvement/image-compression
[IMPROVEMENT] Image compression
parents
ec79b326
a3d71108
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
18 deletions
+93
-18
ChatRoomPresenter.kt
...rocket/android/chatroom/presentation/ChatRoomPresenter.kt
+43
-5
Dialog.kt
app/src/main/java/chat/rocket/android/chatroom/ui/Dialog.kt
+20
-10
MessageParser.kt
...src/main/java/chat/rocket/android/helper/MessageParser.kt
+1
-0
Image.kt
...src/main/java/chat/rocket/android/util/extension/Image.kt
+29
-3
No files found.
app/src/main/java/chat/rocket/android/chatroom/presentation/ChatRoomPresenter.kt
View file @
a9985b28
...
...
@@ -35,6 +35,7 @@ import chat.rocket.android.server.domain.useRealName
import
chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import
chat.rocket.android.server.infraestructure.state
import
chat.rocket.android.util.extension.compressImageAndGetByteArray
import
chat.rocket.android.util.extension.getByteArray
import
chat.rocket.android.util.extension.launchUI
import
chat.rocket.android.util.extensions.avatarUrl
import
chat.rocket.android.util.retryIO
...
...
@@ -80,6 +81,7 @@ import kotlinx.coroutines.experimental.launch
import
kotlinx.coroutines.experimental.withContext
import
org.threeten.bp.Instant
import
timber.log.Timber
import
java.io.InputStream
import
java.util.*
import
javax.inject.Inject
...
...
@@ -346,15 +348,51 @@ class ChatRoomPresenter @Inject constructor(
view
.
showFileSelection
(
settings
.
uploadMimeTypeFilter
())
}
fun
upload
File
(
roomId
:
String
,
uri
:
Uri
,
msg
:
String
,
bitmap
:
Bitmap
?
=
null
)
{
fun
upload
Image
(
roomId
:
String
,
mimeType
:
String
,
uri
:
Uri
,
bitmap
:
Bitmap
,
msg
:
String
)
{
launchUI
(
strategy
)
{
view
.
showLoading
()
try
{
withContext
(
DefaultDispatcher
)
{
val
fileName
=
uriInteractor
.
getFileName
(
uri
)
?:
uri
.
toString
()
val
mimeType
=
uriInteractor
.
getMimeType
(
uri
)
val
byteArray
=
bitmap
?.
compressImageAndGetByteArray
(
mimeType
)
val
fileSize
=
byteArray
?.
size
?:
uriInteractor
.
getFileSize
(
uri
)
if
(
fileName
.
isEmpty
())
{
view
.
showInvalidFileMessage
()
}
else
{
val
byteArray
=
bitmap
.
getByteArray
(
mimeType
,
100
,
settings
.
uploadMaxFileSize
())
retryIO
(
"uploadFile($roomId, $fileName, $mimeType"
)
{
client
.
uploadFile
(
roomId
,
fileName
,
mimeType
,
msg
,
description
=
fileName
)
{
byteArray
.
inputStream
()
}
}
logMediaUploaded
(
mimeType
)
}
}
}
catch
(
ex
:
Exception
)
{
Timber
.
d
(
ex
,
"Error uploading image"
)
when
(
ex
)
{
is
RocketChatException
->
view
.
showMessage
(
ex
)
else
->
view
.
showGenericErrorMessage
()
}
}
finally
{
view
.
hideLoading
()
}
}
}
fun
uploadFile
(
roomId
:
String
,
mimeType
:
String
,
uri
:
Uri
,
msg
:
String
)
{
launchUI
(
strategy
)
{
view
.
showLoading
()
try
{
withContext
(
DefaultDispatcher
)
{
val
fileName
=
uriInteractor
.
getFileName
(
uri
)
?:
uri
.
toString
()
val
fileSize
=
uriInteractor
.
getFileSize
(
uri
)
val
maxFileSizeAllowed
=
settings
.
uploadMaxFileSize
()
when
{
...
...
@@ -370,7 +408,7 @@ class ChatRoomPresenter @Inject constructor(
msg
,
description
=
fileName
)
{
byteArray
?.
inputStream
()
?:
uriInteractor
.
getInputStream
(
uri
)
uriInteractor
.
getInputStream
(
uri
)
}
}
logMediaUploaded
(
mimeType
)
...
...
app/src/main/java/chat/rocket/android/chatroom/ui/Dialog.kt
View file @
a9985b28
...
...
@@ -7,7 +7,7 @@ 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.getMimeType
import
c
om.bumptech.glide.load.resource.gif.GifDrawable
import
c
hat.rocket.common.util.ifNull
import
com.bumptech.glide.request.target.SimpleTarget
import
com.bumptech.glide.request.transition.Transition
...
...
@@ -15,10 +15,12 @@ fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
imagePreview
.
isVisible
=
false
audioVideoAttachment
.
isVisible
=
false
textFile
.
isVisible
=
false
lateinit
var
mimeType
:
String
var
bitmap
:
Bitmap
?
=
null
activity
?.
let
{
context
->
uri
.
getMimeType
(
context
).
let
{
mimeType
->
uri
.
getMimeType
(
context
).
let
{
mimeType
=
it
description
.
text
.
clear
()
when
{
mimeType
.
startsWith
(
"image"
)
->
{
...
...
@@ -27,7 +29,6 @@ fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
.
with
(
context
)
.
asGif
()
.
load
(
uri
)
.
override
(
imagePreview
.
width
,
imagePreview
.
height
)
.
fitCenter
()
.
into
(
imagePreview
)
}
else
{
...
...
@@ -35,7 +36,6 @@ fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
.
with
(
context
)
.
asBitmap
()
.
load
(
uri
)
.
override
(
imagePreview
.
width
,
imagePreview
.
height
)
.
fitCenter
()
.
into
(
object
:
SimpleTarget
<
Bitmap
>()
{
override
fun
onResourceReady
(
...
...
@@ -59,12 +59,22 @@ fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
}
sendButton
.
setOnClickListener
{
presenter
.
uploadFile
(
chatRoomId
,
uri
,
(
citation
?:
""
)
+
description
.
text
.
toString
(),
bitmap
)
bitmap
?.
let
{
bitmap
->
presenter
.
uploadImage
(
chatRoomId
,
mimeType
,
uri
,
bitmap
,
(
citation
?:
""
)
+
description
.
text
.
toString
()
)
}.
ifNull
{
presenter
.
uploadFile
(
chatRoomId
,
mimeType
,
uri
,
(
citation
?:
""
)
+
description
.
text
.
toString
()
)
}
alertDialog
.
dismiss
()
}
cancelButton
.
setOnClickListener
{
alertDialog
.
dismiss
()
}
...
...
app/src/main/java/chat/rocket/android/helper/MessageParser.kt
View file @
a9985b28
...
...
@@ -13,6 +13,7 @@ import android.text.style.ReplacementSpan
import
android.view.View
import
androidx.core.content.res.ResourcesCompat
import
chat.rocket.android.R
import
androidx.core.util.PatternsCompat
import
chat.rocket.android.chatroom.ui.StrikethroughDelimiterProcessor
import
chat.rocket.android.emoji.EmojiParser
import
chat.rocket.android.emoji.EmojiRepository
...
...
util/src/main/java/chat/rocket/android/util/extension/Image.kt
View file @
a9985b28
...
...
@@ -32,20 +32,46 @@ suspend fun Bitmap.compressImageAndGetInputStream(mimeType: String): InputStream
return
inputStream
}
/**
* Returns a [ByteArray] of a [Bitmap].
*
* @param mimeType The MIME type of the [Bitmap].
* @param quality The quality of the [Bitmap] for the resulting [ByteArray].
* @param maxFileSizeAllowed The max file size allowed by the server. Note: The [quality] will be
* decreased minus 10 until the [ByteArray] size fits the [maxFileSizeAllowed] value.
* @return A [ByteArray] of a [Bitmap]
*/
suspend
fun
Bitmap
.
getByteArray
(
mimeType
:
String
,
quality
:
Int
,
maxFileSizeAllowed
:
Int
):
ByteArray
{
lateinit
var
byteArray
:
ByteArray
compressImageAndGetByteArray
(
mimeType
,
quality
)
?.
let
{
if
(
it
.
size
>
maxFileSizeAllowed
&&
maxFileSizeAllowed
!
in
-
1
..
0
)
{
getByteArray
(
mimeType
,
quality
-
10
,
maxFileSizeAllowed
)
}
else
{
byteArray
=
it
}
}
return
byteArray
}
/**
* Compress a [Bitmap] image.
*
* @param mimeType The MimeType of what the compressed image should be.
* @return An [ByteArray] of a compressed image, otherwise null if the compression couldn't be done.
*/
suspend
fun
Bitmap
.
compressImageAndGetByteArray
(
mimeType
:
String
):
ByteArray
?
{
suspend
fun
Bitmap
.
compressImageAndGetByteArray
(
mimeType
:
String
,
quality
:
Int
=
100
):
ByteArray
?
{
var
byteArray
:
ByteArray
?
=
null
withContext
(
DefaultDispatcher
)
{
val
byteArrayOutputStream
=
ByteArrayOutputStream
()
// TODO: Add an option the the app to the user be able to select the quality of the compressed image
val
isCompressed
=
this
.
compress
(
mimeType
.
getCompressFormat
(),
70
,
byteArrayOutputStream
)
this
.
compress
(
mimeType
.
getCompressFormat
(),
quality
,
byteArrayOutputStream
)
if
(
isCompressed
)
{
byteArray
=
byteArrayOutputStream
.
toByteArray
()
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment