I have an app that shares images from url. Last android update, I got message from instagram \"Unable to load image\" when I want to share an image in instagram feed.
Looks like Facebook already has the bug for this issue: https://developers.facebook.com/support/bugs/1326888287510350/
As a temporary workaround you can save media to MediaStore. This is the method we use to store and then return the uri for Instagram sharing.
private fun insertImageToMediaStore(file: File): Uri? {
val fileUri = FileProvider.getUriForFile(
context,
"${context.applicationContext.packageName}.provider",
file
)
val mimeType = context.contentResolver.getType(fileUri) ?: "image/*"
val isImage = mimeType.contains("image")
val values = ContentValues().apply {
put(
if (isImage) {
MediaStore.Images.Media.DISPLAY_NAME
} else {
MediaStore.Video.Media.DISPLAY_NAME
},
file.name
)
put(
if (isImage) {
MediaStore.Images.Media.MIME_TYPE
} else {
MediaStore.Video.Media.MIME_TYPE
},
mimeType
)
}
val collection = if (isImage) {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
} else {
MediaStore.Video.Media.EXTERNAL_CONTENT_URI
}
val uri = context.contentResolver.insert(collection, values)
uri?.let {
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
try {
outputStream.write(file.readBytes())
outputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
values.clear()
} ?: throw RuntimeException("MediaStore failed for some reason")
return uri
}