Allow to pick multiple files in chat message picker
This commit is contained in:
parent
3159ed7ab3
commit
9a548ff388
4 changed files with 76 additions and 25 deletions
|
@ -338,16 +338,14 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
|||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
lifecycleScope.launch {
|
||||
val fileToUploadPath = ImageUtils.getImageFilePathFromPickerIntent(
|
||||
data,
|
||||
chatSendingViewModel.temporaryFileUploadPath
|
||||
)
|
||||
for (fileToUploadPath in ImageUtils.getFilesPathFromPickerIntent(data, chatSendingViewModel.temporaryFileUploadPath)) {
|
||||
if (fileToUploadPath != null) {
|
||||
chatSendingViewModel.addAttachment(fileToUploadPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun enterEditionMode() {
|
||||
listSelectionViewModel.isEditionEnabled.value = true
|
||||
|
@ -481,6 +479,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
|||
val galleryIntent = Intent(Intent.ACTION_PICK)
|
||||
galleryIntent.type = "*/*"
|
||||
galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*"))
|
||||
galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||
|
||||
if (PermissionHelper.get().hasCameraPermission()) {
|
||||
// Allows to capture directly from the camera
|
||||
|
@ -497,6 +496,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
|||
// Finally allow any kind of file
|
||||
val fileIntent = Intent(Intent.ACTION_GET_CONTENT)
|
||||
fileIntent.type = "*/*"
|
||||
fileIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
|
||||
cameraIntents.add(fileIntent)
|
||||
}
|
||||
|
||||
|
|
|
@ -132,7 +132,7 @@ class ContactEditorFragment : GenericFragment<ContactEditorFragmentBinding>(), S
|
|||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
lifecycleScope.launch {
|
||||
val contactImageFilePath = ImageUtils.getImageFilePathFromPickerIntent(data, temporaryPicturePath)
|
||||
val contactImageFilePath = ImageUtils.getFilePathFromPickerIntent(data, temporaryPicturePath)
|
||||
if (contactImageFilePath != null) {
|
||||
viewModel.setPictureFromPath(contactImageFilePath)
|
||||
}
|
||||
|
|
|
@ -120,7 +120,7 @@ class SideMenuFragment : GenericFragment<SideMenuFragmentBinding>() {
|
|||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
lifecycleScope.launch {
|
||||
val contactImageFilePath = ImageUtils.getImageFilePathFromPickerIntent(data, temporaryPicturePath)
|
||||
val contactImageFilePath = ImageUtils.getFilePathFromPickerIntent(data, temporaryPicturePath)
|
||||
if (contactImageFilePath != null) {
|
||||
viewModel.setPictureFromPath(contactImageFilePath)
|
||||
}
|
||||
|
|
|
@ -71,10 +71,25 @@ class ImageUtils {
|
|||
return ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND)
|
||||
}
|
||||
|
||||
suspend fun getImageFilePathFromPickerIntent(data: Intent?, temporaryImageFilePath: File?): String? {
|
||||
suspend fun getFilesPathFromPickerIntent(data: Intent?, temporaryImageFilePath: File?): List<String> {
|
||||
var imageFilePath: String? = null
|
||||
if (temporaryImageFilePath != null) {
|
||||
if (data != null) {
|
||||
val clipData = data.clipData
|
||||
if (clipData != null) { // Multiple selection
|
||||
Log.i("[Image Utils] Found ${clipData.itemCount} elements")
|
||||
val list = arrayListOf<String>()
|
||||
for (i in 0 until clipData.itemCount) {
|
||||
val dataUri = clipData.getItemAt(i).uri
|
||||
if (dataUri != null) {
|
||||
imageFilePath = dataUri.toString()
|
||||
Log.i("[Image Utils] Using data URI $imageFilePath")
|
||||
}
|
||||
imageFilePath = cleanFilePath(imageFilePath)
|
||||
if (imageFilePath != null) list.add(imageFilePath)
|
||||
}
|
||||
return list
|
||||
} else { // Single selection
|
||||
val dataUri = data.data
|
||||
if (dataUri != null) {
|
||||
imageFilePath = dataUri.toString()
|
||||
|
@ -83,26 +98,62 @@ class ImageUtils {
|
|||
imageFilePath = temporaryImageFilePath.absolutePath
|
||||
Log.i("[Image Utils] Data URI is null, using $imageFilePath")
|
||||
}
|
||||
imageFilePath = cleanFilePath(imageFilePath)
|
||||
if (imageFilePath != null) return arrayListOf(imageFilePath)
|
||||
}
|
||||
} else if (temporaryImageFilePath.exists()) {
|
||||
imageFilePath = temporaryImageFilePath.absolutePath
|
||||
Log.i("[Image Utils] Data is null, using $imageFilePath")
|
||||
imageFilePath = cleanFilePath(imageFilePath)
|
||||
if (imageFilePath != null) return arrayListOf(imageFilePath)
|
||||
}
|
||||
}
|
||||
return arrayListOf()
|
||||
}
|
||||
|
||||
suspend fun getFilePathFromPickerIntent(data: Intent?, temporaryImageFilePath: File?): String? {
|
||||
var imageFilePath: String? = null
|
||||
if (temporaryImageFilePath != null) {
|
||||
if (data != null) {
|
||||
val clipData = data.clipData
|
||||
if (clipData != null) { // Multiple selection
|
||||
for (i in 0 until clipData.itemCount) {
|
||||
val uri = clipData.getItemAt(i).uri
|
||||
}
|
||||
} else { // Single selection
|
||||
val dataUri = data.data
|
||||
if (dataUri != null) {
|
||||
imageFilePath = dataUri.toString()
|
||||
Log.i("[Image Utils] Using data URI $imageFilePath")
|
||||
} else if (temporaryImageFilePath.exists()) {
|
||||
imageFilePath = temporaryImageFilePath.absolutePath
|
||||
Log.i("[Image Utils] Data URI is null, using $imageFilePath")
|
||||
}
|
||||
}
|
||||
} else if (temporaryImageFilePath.exists()) {
|
||||
imageFilePath = temporaryImageFilePath.absolutePath
|
||||
Log.i("[Image Utils] Data is null, using $imageFilePath")
|
||||
}
|
||||
}
|
||||
|
||||
if (imageFilePath != null) {
|
||||
if (imageFilePath.startsWith("content://") ||
|
||||
imageFilePath.startsWith("file://")
|
||||
) {
|
||||
val uriToParse = Uri.parse(imageFilePath)
|
||||
imageFilePath = FileUtils.getFilePath(coreContext.context, uriToParse)
|
||||
Log.i("[Image Utils] Path was using a content or file scheme, real path is: $imageFilePath")
|
||||
if (imageFilePath == null) {
|
||||
Log.e("[Image Utils] Failed to get access to file $uriToParse")
|
||||
}
|
||||
}
|
||||
return cleanFilePath(imageFilePath)
|
||||
}
|
||||
|
||||
return imageFilePath
|
||||
private suspend fun cleanFilePath(filePath: String?): String? {
|
||||
if (filePath != null) {
|
||||
if (filePath.startsWith("content://") ||
|
||||
filePath.startsWith("file://")
|
||||
) {
|
||||
val uriToParse = Uri.parse(filePath)
|
||||
val result = FileUtils.getFilePath(coreContext.context, uriToParse)
|
||||
Log.i("[Image Utils] Path was using a content or file scheme, real path is: $filePath")
|
||||
if (result == null) {
|
||||
Log.e("[Image Utils] Failed to get access to file $uriToParse")
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
return filePath
|
||||
}
|
||||
|
||||
private fun getRoundBitmap(bitmap: Bitmap): Bitmap? {
|
||||
|
|
Loading…
Reference in a new issue