Skip to content

Commit

Permalink
Internalizing shared image temprorarily into cache and using its cont…
Browse files Browse the repository at this point in the history
…ent uri to pre-view the image.
  • Loading branch information
Scapesfear committed Jan 21, 2025
1 parent cbebed6 commit 4d397a5
Showing 1 changed file with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -582,17 +582,43 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
*/
private fun WebView.loadImage(imageUri: Uri) {
Timber.i("Loading non-SVG image using WebView")
val imagePath = imageUri.toString()
val htmlData =
"""
<html>
<body style="margin:0;padding:0;">
<img src="$imagePath" style="width:100%;height:auto;" />
</body>
</html>
""".trimIndent()

loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null)
try {
val internalFile = internalizeUri(imageUri)?.takeIf { it.exists() }
if (internalFile == null) {
Timber.w(
"loadImage() unable to internalize image from Uri %s",
imageUri,
)
showSomethingWentWrong()
}

val contentUri = internalFile?.let { getContentUriFromFile(it) }
if (contentUri == null) {
Timber.w("Failed to get content URI for the image.")
showSomethingWentWrong()
}

val htmlData =
"""
<html>
<body style="margin:0;padding:0;">
<img src="$contentUri" style="width:100%;height:auto;" />
</body>
</html>
""".trimIndent()

loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null)
} catch (e: Exception) {
Timber.e(e, "Error loading image in WebView")
showSomethingWentWrong()
}
}

private fun getContentUriFromFile(file: File): Uri? {
val context = context ?: return null
val authority = context.applicationContext.packageName + ".apkgfileprovider"
return FileProvider.getUriForFile(context, authority, file)
}

/** Shows an error image along with an error text **/
Expand Down

0 comments on commit 4d397a5

Please sign in to comment.