I am trying to use nearby share in kotlin to send some strings and an image from one device to another but I've been having the same problem for over a month, basically what im trying to do is share some information about a user and then display it in another device, and I have no problem with the strings, but with the image, which I want to display after receiving, I'm getting this error: Failed to create image decoder with message 'input was incomplete', it's very weird, because sometimes it works -> this is what i should get, others it doesn't -> failed image, and others it just works halfway literally -> half of the image.
The code for receiving the image:
val uri = filePayloads[0].asFile()!!.asUri()!!
val file = getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + "/${endpointIdUserId.get(endpointId)}-profile-picture-" + System.currentTimeMillis().toString() + ".jpeg"
File(file)
val inp: InputStream? = contentResolver.openInputStream(uri)
copyStream(inp!!, FileOutputStream(file))
contentResolver.delete(uri, null, null)
val user = users.find { it.id == endpointIdUserId.get(endpointId) }
user!!.pfp = file
addPayload(endpointId)
filePayloads.remove(filePayloads[0])
The code for displaying the image in a fragment (getting it from a room database):
if(currentItem.pfp != null) {
holder.pfpImageView.setImageURI(Uri.parse(currentItem.pfp))
}
I would really appreciate any help, thanks guys
You should properly manage and close your resources. Most Streams will flush on close, which makes sure all data is written to their target (the file, in this case):