Android 图像保存在应用程序文件夹中,大小为0字节(以Kotlin为单位)?
我正在使用第三方库打开图库和相机。我 我已经完成了那部分。现在,当我选择多个图像或单个 image,从第三方库获取URI数组。现在,我创建了这个文件 应用程序包文件夹,并能够创建它。但当我检查下 应用程序文件夹,图像大小为0字节。我也在保存路径 在本地数据库和更高版本上,将使用multipart将其上载到服务器上。在下面 这是我的密码 打开多媒体资料和照相机Android 图像保存在应用程序文件夹中,大小为0字节(以Kotlin为单位)?,android,image,kotlin,Android,Image,Kotlin,我正在使用第三方库打开图库和相机。我 我已经完成了那部分。现在,当我选择多个图像或单个 image,从第三方库获取URI数组。现在,我创建了这个文件 应用程序包文件夹,并能够创建它。但当我检查下 应用程序文件夹,图像大小为0字节。我也在保存路径 在本地数据库和更高版本上,将使用multipart将其上载到服务器上。在下面 这是我的密码 打开多媒体资料和照相机 private fun openPicker() { PhotoPickerFragment.newInstance(
private fun openPicker() {
PhotoPickerFragment.newInstance(
multiple = true,
allowCamera = true,
maxSelection = 5,
theme = R.style.ChiliPhotoPicker_Light
).show(childFragmentManager, "picker")
}
已获取所选图像路径URI,并使用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
)
}
}
以下是库URL:您的createImageFile()
函数通过返回的结果而不是图像文件来创建空文件。您不会将任何内容写入该空文件并将其用作附件。而且
// Get the file-name from the image-path
val destinationFilePath = it[index].absolutePath
您不使用评论中所说的图像路径;使用刚创建的空文件的路径
图像文件在中以Uri
,e
的形式提供给您
photos.forEachIndexed { index, e ->
但你忽略它除了记录
println("$e at ${photos[index].path}")
所以
用它来工作。或者,如果您想要的话,也可以使用
createImageFile
创建的文件(Java有答案,但IDEA/Android Studio可以为您将其转换为Kotlin)。好吧,您的代码使用createTempFile
创建了一个空文件,并且似乎没有写入任何内容。如果addAttachFromApi
应该这样做,那么就显示出来@是的。我可以在package->files下看到0字节的文件。attachImageviewModel?.addAttachFromApi(附件)此行将图像从设备添加到本地数据库。我想我必须复制文件时,图像捕获或图像选择在包->文件文件夹
println("$e at ${photos[index].path}")
val destinationFile = File(e.path)