【发布时间】:2011-07-23 10:57:08
【问题描述】:
如何将本地图像加载到 Web 视图中? webview 中的内容来自互联网,我想用我的本地驱动器路径替换 img src?有可能吗?
【问题讨论】:
如何将本地图像加载到 Web 视图中? webview 中的内容来自互联网,我想用我的本地驱动器路径替换 img src?有可能吗?
【问题讨论】:
您需要下载源代码并保存 html 文件,我建议您使用 ByteArrayInputStream 在内存中执行此操作。然后打开它并替换“
您也可以使用 Intent 要求下载器下载 html 文件。
一个警告,不要在你的 UI 线程上下载文件,你真的不想在下载完成之前停止它。
【讨论】:
是的,有可能……! 你可以像这样在你的html文件中设置你的源代码
src="file://mnt/sdcard/images/panda.jpg"
并将您的文件放在您的
“sdcard/images”文件夹
或
您可以将图像保存在资产文件夹中,并将您的源代码设置为 html 为
file:///android_asset/panda.jpg
注意:确保你的路径是正确的......否则它不会工作.. :)
干杯!
【讨论】:
将您的徽标放入资产目录 示例:assets/logo.png
然后
String htmlData="<img src=logo.png>";
webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", null);
【讨论】:
2021年,我是这样做的:
在MainActivity中,定义assetLoader:
private lateinit var assetLoader: WebViewAssetLoader
在 MainActivity.onCreate() 中,定义您想要为 webview 资产加载器提供的本地目录:
val publicDir: File = File(this.applicationContext.cacheDir, "public")
assetLoader = WebViewAssetLoader.Builder()
.setDomain("you website domain")
.addPathHandler("/public/", InternalStoragePathHandler(this.applicationContext, publicDir))
.build()
mWebView.webViewClient = object : WebViewClientCompat() {
@RequiresApi(21)
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
val res: WebResourceResponse? = assetLoader.shouldInterceptRequest(request.url)
return res
}
// to support API < 21
override fun shouldInterceptRequest(
view: WebView,
url: String
): WebResourceResponse? {
return assetLoader.shouldInterceptRequest(Uri.parse(url))
}
}
将 onActivityResult() 添加到 MainActivity:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
val result = CropImage.getActivityResult(data)
if (resultCode == RESULT_OK) {
val resultUri: Uri = result.uri
val filePath = resultUri.path
// parse the filePath to find the directory and the file name. Note you may need to change the number 22 to some other number to find the delimiter / between the path and the file name.
val index: Int = filePath!!.indexOf("com.your.package/cache") + 22
val imageFileName = filePath.substring(index)
val imageFile = File(filePath)
// move the cropped image to the public subdirectory
val publicFileDir = File(filePath.substring(0, index) + "public/")
publicFileDir.mkdirs()
imageFile.renameTo(File(publicFileDir, imageFileName))
// change the image src in webview
val imageUrl: String = "/public/" + imageFileName
mWebView.evaluateJavascript("$('#image').attr('src', '$imageUrl');", null)
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
val error = result.error
Toast.makeText(this@MainActivity, "Error in picking an image.", Toast.LENGTH_LONG).show()
}
}
}
请注意,我将“com.your.package/cache/public”作为“/public”作为 webview 可用的资产文件夹。换句话说,webview 会将https://your.domain/public/image_file 解释为 com.your.package/cache/public/image_file 中的本地文件。 WebViewAssetLoader 的意义在于,现在您的本地文件将可以通过与您服务器的 html 文件位于同一域中的 URL 访问,从而绕过同源策略 (SOP) 限制。
【讨论】: