Android 从 WebView Javascript 访问本地文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4285791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:53:39  来源:igfitidea点击:

Android Access Local File From WebView Javascript

javascriptandroidwebview

提问by user163757

I am looking for an elegant way to access local files (either on phone memory or SD Card) on an Android device from JavaScript running in WebView on an Android device.

我正在寻找一种优雅的方式从在 Android 设备上的 WebView 中运行的 JavaScript 访问 Android 设备上的本地文件(在手机内存或 SD 卡上)。

I am loading said WebView with a html page from the device located in the assets folder. Along these lines, I believe I can access any file in this assets folder from the Javascript, but can't load anything from SD card or phone memory. I am aware of the addJavaScriptInterface, but I am specifically trying to load images, so using this method would require some tweaking to pass a B64 encoded image.

我正在从位于资产文件夹中的设备加载带有 html 页面的 WebView。按照这些思路,我相信我可以从 Javascript 访问此资产文件夹中的任何文件,但无法从 SD 卡或手机内存加载任何文件。我知道 addJavaScriptInterface,但我专门尝试加载图像,因此使用此方法需要进行一些调整才能传递 B64 编码的图像。

I am new to Android development, and want to make sure I am not missing something obvious.

我是 Android 开发的新手,想确保我没有遗漏一些明显的东西。

采纳答案by basicsharp

You need to implement a ContentProvider to map local files to uris.

您需要实现一个 ContentProvider 来将本地文件映射到 uri。