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
4c8e492a
Commit
4c8e492a
authored
Oct 29, 2018
by
Filipe de Lima Brito
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improves the image upload process.
parent
ce646a38
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
16 deletions
+92
-16
ChatRoomPresenter.kt
...rocket/android/chatroom/presentation/ChatRoomPresenter.kt
+43
-5
Dialog.kt
app/src/main/java/chat/rocket/android/chatroom/ui/Dialog.kt
+20
-8
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 @
4c8e492a
...
...
@@ -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 @
4c8e492a
...
...
@@ -7,6 +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
chat.rocket.common.util.ifNull
import
com.bumptech.glide.request.target.SimpleTarget
import
com.bumptech.glide.request.transition.Transition
...
...
@@ -14,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"
)
->
{
...
...
@@ -25,7 +28,6 @@ fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
.
with
(
context
)
.
asBitmap
()
.
load
(
uri
)
.
override
(
imagePreview
.
width
,
imagePreview
.
height
)
.
fitCenter
()
.
into
(
object
:
SimpleTarget
<
Bitmap
>()
{
override
fun
onResourceReady
(
...
...
@@ -48,12 +50,22 @@ fun ChatRoomFragment.showFileAttachmentDialog(uri: Uri) {
}
sendButton
.
setOnClickListener
{
bitmap
?.
let
{
bitmap
->
presenter
.
uploadImage
(
chatRoomId
,
mimeType
,
uri
,
bitmap
,
(
citation
?:
""
)
+
description
.
text
.
toString
()
)
}.
ifNull
{
presenter
.
uploadFile
(
chatRoomId
,
mimeType
,
uri
,
(
citation
?:
""
)
+
description
.
text
.
toString
(),
bitmap
(
citation
?:
""
)
+
description
.
text
.
toString
()
)
}
alertDialog
.
dismiss
()
}
cancelButton
.
setOnClickListener
{
alertDialog
.
dismiss
()
}
...
...
util/src/main/java/chat/rocket/android/util/extension/Image.kt
View file @
4c8e492a
...
...
@@ -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