小编Jch*_*han的帖子

无法在 API 级别 30 上加载本地 html 文件

我的应用程序加载位于getFilesDir()via下的本地 html 文件WebView#loadUrl()
之前targetSdkVersion = 29,下面的代码正在运行。

        copyAssetsFile(this, "sample.html", getFilesDir().getAbsolutePath());
        webView.getSettings().setJavaScriptEnabled(true);
        String url = "file://" + getFilesDir().getAbsolutePath() + "/sample.html";
        webView.loadUrl(url);
    }

    private static void copyAssetsFile(Context context, String fileName, String directoryPath) {
        try {
            InputStream inputStream = context.getResources().getAssets().open(fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(
                    new File(directoryPath, fileName), false);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buffer)) >= 0) {
                fileOutputStream.write(buffer, 0, length);
            }

            fileOutputStream.close();
            inputStream.close();
Run Code Online (Sandbox Code Playgroud)

完整示例在这里

但是,它在更改后不起作用targetSdkVersion = 30 …

android android-webview android-11

7
推荐指数
1
解决办法
1344
查看次数

标签 统计

android ×1

android-11 ×1

android-webview ×1