我刚刚将图像保存到:App.getContext().filesDir
现在,我想像这样将html中的图像加载到Webview:
<img src="/data/user/0/com.asd.myappname/files/MyAppName/htmlimgs/first.jpg" />
图像像这里一样破裂。为什么?
但是img在文件夹中...
答案 0 :(得分:1)
您可以编写此代码并尝试
web.getSettings().setDomStorageEnabled(true);
OR:
获取图像的位图并将其转换为base64编码的图像,然后将其显示为WebView,
Bitmap bitmap = BitmapFactory.decodeFile("/data/user/0/com.asd.myappname/files/MyAppName/htmlimgs/first.jpg");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bitmap);
// Desired Bitmap and the html code, where you want to place it
Bitmap bitmap = YOUR_BITMAP;
String html="<html><body><img src='{IMAGE_PLACEHOLDER}' /></body> </html>";
// Convert bitmap to Base64 encoded image for web
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
String image = "data:image/png;base64," + imgageBase64;
// Use image for the img src parameter in your html and load to webview
html = html.replace("{IMAGE_PLACEHOLDER}", image);
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "");
尝试一下。
答案 1 :(得分:1)
在kotlin中尝试此代码(来自Sana答案)
您可以编写此代码并尝试,
web.getSettings().setDomStorageEnabled(true)
(OR)
val bitmap = BitmapFactory.decodeFile("/data/user/0/com.asd.myappname/files/MyAppName/htmlimgs/first.jpg")
val imgView = this.findViewById(R.id.imgViewId) as ImageView
imgView.setImageBitmap(bitmap)
val bitmap = YOUR_BITMAP
val html = "<html><body><img src='{IMAGE_PLACEHOLDER}' /></body> </html>"
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
val byteArray = byteArrayOutputStream.toByteArray()
val imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT)
val image = "data:image/png;base64," + imgageBase64
html = html.replace("{IMAGE_PLACEHOLDER}", image)
webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "")