Reputation: 2223
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
Upvotes: 0
Views: 929
Reputation: 170723
Your createImageFile()
function creates an empty file by returning a result of createTempFile
, not an image file. You don't write anything to that empty file and use it as the attachment. And in
// Get the file-name from the image-path
val destinationFilePath = it[index].absolutePath
you don't use the image path as the comment says; you use the path of the empty file you just created.
The image file is given to you as Uri
, e
in
photos.forEachIndexed { index, e ->
but you ignore it except to log
println("$e at ${photos[index].path}")
val destinationFile = File(e.path)
and work with that. Or copy it to the file created by createImageFile
if that's what you want (answers there are for Java but IDEA/Android Studio can convert them to Kotlin for you).
Upvotes: 1