I am using the third party library to open the Gallery and camera. I have done that part. Now, when i select multiple image or single image,got the array of URI from the third party lib. Now, i created the file in app package folder and able to create it. But when i check under app folder, the size of an image is 0 byte. I am saving the path also on local db and later will upload it on server using multipart. Below is my code.
To open the Gallery and camera
private fun openPicker() {
PhotoPickerFragment.newInstance(
multiple = true,
allowCamera = true,
maxSelection = 5,
theme = R.style.ChiliPhotoPicker_Light
).show(childFragmentManager, "picker")
}
got the selected image path URI and save path in to local db with createFile
override fun onImagesPicked(photos: ArrayList<Uri>) {
Log.e("TAG", "pic" + photos.joinToString(separator = "\n") { it.toString() })
fileList = ArrayList<File>()
try {
photos.forEachIndexed { index, e ->
println("$e at ${photos[index].path}")
val destinationFile: File = createImageFile()
fileList.add(destinationFile)
fileList.also {
// Get the file-name from the image-path
val destinationFilePath = it[index].absolutePath
val fileName =
destinationFilePath.substring(destinationFilePath.lastIndexOf("/") + 1)
val attachment = AttachSiteImage()
attachment.apply {
callLoggingId = callLoggingIdForAttachment
attachmentFileName = fileName
attachmentPath = destinationFilePath
}
attachImageviewModel?.addAttachFromApi(attachment)
}
}
Log.e("TAG", "Path->" + fileList.size)
} catch (ex: FileAlreadyExistsException) {
// sourceFile.delete()
cl_attachments_main_container.showSnackBar(
if (!ex.localizedMessage.isNullOrEmpty())
ex.localizedMessage
else
ex.stackTrace.toString(),
Snackbar.LENGTH_SHORT
)
} catch (ex: IOException) {
// sourceFile.delete()
cl_attachments_main_container.showSnackBar(
if (!ex.localizedMessage.isNullOrEmpty())
ex.localizedMessage
else
ex.stackTrace.toString(),
Snackbar.LENGTH_SHORT
)
}
}
Create the file where photos will be stored
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File? = requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"${callLoggingIdForAttachment}_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
)
}
Here is the Library URL: https://github.com/ChiliLabs/ChiliPhotoPicker