Android WebView 从内部存储加载 |安卓 11 |安卓

标签 android

实际上我的要求是我想将动态文件加载到像图像一样的网页中。视频,音频等,它来自 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 的输出,
enter image description here
看,从 Asset 和 files 目录都按预期加载。
这里是 30,
enter image description here
这里仅从 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 的输出
enter image description here
如果我将 SdkVersion 更改为 compileSdkVersion 30还有targetSdkVersion 30这给了我访问被拒绝的错误,
30 日输出
enter image description here
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/

相关文章:

android - 如何打开ActivityGroup的子Activity

android - MediaController 导致 java.lang.SecurityException : Permission Denial

android - 我可以使用 D'Fusion 作为 Metaio 的增强现实创作工具吗?

Android Lollipop 首选项

android - 如何在 Android 中为 Navigation Drawer 添加一个分区分隔符?

java - 位置管理器的 AddGpsStatusListener 在 Android 10 中不起作用

android - 如何在 ARMovieProj (ARToolkit) 中更改视频的大小

android - OpenSL ES 解码 24 位 FLAC

android - 在 Android 应用程序中显示来自计算机的真实设备中的图像

android.intent.action.BATTERY_OKAY 在广播接收器中不起作用,在广播接收器中该怎么办?