Skip to content

Commit b0d3758

Browse files
Scapesfearlukstbit
authored andcommitted
Internalizing shared image temprorarily into cache and using its content uri to pre-view the image.
1 parent d2f499b commit b0d3758

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/multimedia/MultimediaImageFragment.kt

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,45 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
582582
*/
583583
private fun WebView.loadImage(imageUri: Uri) {
584584
Timber.i("Loading non-SVG image using WebView")
585-
val imagePath = imageUri.toString()
586-
val htmlData =
587-
"""
588-
<html>
589-
<body style="margin:0;padding:0;">
590-
<img src="$imagePath" style="width:100%;height:auto;" />
591-
</body>
592-
</html>
593-
""".trimIndent()
594585

595-
loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null)
586+
try {
587+
val internalFile = internalizeUri(imageUri)?.takeIf { it.exists() }
588+
if (internalFile == null) {
589+
Timber.w(
590+
"loadImage() unable to internalize image from Uri %s",
591+
imageUri,
592+
)
593+
showSomethingWentWrong()
594+
return
595+
}
596+
597+
val contentUri = getContentUriFromFile(internalFile)
598+
if (contentUri == null) {
599+
Timber.w("Failed to get content URI for the image.")
600+
showSomethingWentWrong()
601+
return
602+
}
603+
604+
val htmlData =
605+
"""
606+
<html>
607+
<body style="margin:0;padding:0;">
608+
<img src="$contentUri" style="width:100%;height:auto;" />
609+
</body>
610+
</html>
611+
""".trimIndent()
612+
613+
loadDataWithBaseURL(null, htmlData, "text/html", "UTF-8", null)
614+
} catch (e: Exception) {
615+
Timber.e(e, "Error loading image in WebView")
616+
showSomethingWentWrong()
617+
}
618+
}
619+
620+
private fun getContentUriFromFile(file: File): Uri? {
621+
val context = context ?: return null
622+
val authority = context.applicationContext.packageName + ".apkgfileprovider"
623+
return FileProvider.getUriForFile(context, authority, file)
596624
}
597625

598626
/** Shows an error image along with an error text **/

0 commit comments

Comments
 (0)