实际上我的要求是我想将动态文件加载到像图像一样的网页中。视频,音频等,它来自 Assets 或应用程序自己的文件目录,这不是问题。
我尝试使用以下两种方式。
方式一
这是我的 html 文件在 assets 文件夹中,我有 123.jpg
在 Assets 文件夹和内部文件夹中,
主页.html,
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Image testing</h1>
<p>Relative Path</p>
<img src="123.jpg" alt="Italian Trulli" width="100%">
<p>From Files Directory</p>
<img src="file:////data/user/0/com.guna.testapplication/files/123.jpg" alt="Italian Trulli" width="100%">
</body>
</html>
我正在将它加载到 webview 中webView.loadUrl("file:///android_asset/Home.html")
这是我 29 的输出,看,从 Asset 和 files 目录都按预期加载。
这里是 30,
这里仅从 Assets 目录加载。并且文件目录未加载。
方式二
在这里,我从内部存储加载 html 和图像。
主页.html
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Image testing</h1>
<img src="123.jpg" alt="Italian Trulli" width="100%">
</body>
</html>
我正在像这样加载webview,webView.loadUrl("file:////data/user/0/com.guna.testapplication/files/Home.html")
如果我的 compileSdkVersion 29
还有targetSdkVersion 29
这很好用,29 的输出
如果我将 SdkVersion 更改为
compileSdkVersion 30
还有targetSdkVersion 30
这给了我访问被拒绝的错误,30 日输出
list
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
最佳答案
找到答案,
来自 setAllowFileAccess我们必须将其显式设置为 true
当我们瞄准 Build.VERSION_CODES.R ,否则将不允许您加载 file:// URLs
.
所以解决方案是
webView.settings.allowFileAccess = true
在 Java 中,webView.getSettings().setAllowFileAccess(true);
这在两种情况下都按预期工作。
关于Android WebView 从内部存储加载 |安卓 11 |安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63830399/