Java 使用Android Studio从assets文件夹将html文件加载到android上的webview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28549420/
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-08-11 06:24:05  来源:igfitidea点击:

Loading html file to webview on android from assets folder using Android Studio

javaandroidwebviewandroid-studiogradle

提问by Badr Hari

I'm using Android Studio/Gradle.

我正在使用 Android Studio/Gradle。

app\src\main\android_asset folder has the file called chart.html..

app\src\main\android_asset 文件夹中有名为 chart.html 的文件。

I'm trying to load this file to my webview like this:

我正在尝试将此文件加载到我的 webview 中:

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/chart.html");
setContentView(view);

But I always get the error: could not be loaded because ERR_FILE_NOT_FOUND.

但我总是收到错误:无法加载,因为 ERR_FILE_NOT_FOUND。

What am I missing here?

我在这里缺少什么?

采纳答案by Rustam

The directory name should be assetsnot android_assets

目录名称应该是assets而不是android_assets

Do like this: enter image description here

这样做: enter image description here

As shown in the above pics just right click on your app->New->Folder->Assets Folder

如上图所示,只需右键单击您的应用程序- >新建->文件夹->资产文件夹

Now put your .htmlfile here in assetsfolder.

现在将您的.html文件放在assets文件夹中。

That's it. Done.

就是这样。完毕。

Remaining is same in code what you did.

代码中的其余部分与您所做的相同。

WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/hello.html");
setContentView(view);

回答by Gugelhupf

Similar problem :

类似问题:

I use many productFlavors with different applicationId.

我使用了许多具有不同 applicationId 的 productFlavors。

If I attempt to load a html file from res/raw/file.html I get a ClassNotFoundExceptionDidn't find class "product.flavor.package.R$raw"

如果我尝试从 res/raw/file.html 加载 html 文件,我会收到ClassNotFoundException未找到类“product.flavor.package.R$raw”

The R.java file has a different Package name.

R.java 文件具有不同的包名称。

It look's like you can't load a URL from file like : webView.loadUrl( "file:///android_res/raw/page.html"); because the WebView try to use the R.class file with has a different package name.

看起来您无法从文件中加载 URL,例如: webView.loadUrl( "file:///android_res/raw/page.html"); 因为 WebView 尝试使用具有不同包名的 R.class 文件。

I assume the ERR_FILE_NOT_FOUND from loading a html file from assets has the same problem but you don't see the exception. ( webView.loadUrl( "file:///android_assets/page.html" ); )

我假设从资产加载 html 文件的 ERR_FILE_NOT_FOUND 有同样的问题,但你没有看到异常。( webView.loadUrl( "file:///android_assets/page.html");)

With this small work around I solve my problem :

通过这个小小的工作,我解决了我的问题:

try {
        AssetManager assetManager = context.getAssets();
        InputStream stream = assetManager.open("page.html");
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line).append("\n");
        }
        webView.loadDataWithBaseURL(null, total.toString(), "text/html", "UTF-8", null);
    } catch (Exception xxx) {
        Log.e(TAG, "Load assets/page.html", xxx);
    }

I hope this helps. Stephan

我希望这有帮助。斯蒂芬

回答by Harry

Answer from Gugelhupfbut with raw resource.
Advantage from this solution: you keep translation working!

来自Gugelhupf 的回答,但使用原始资源。
此解决方案的优势:您可以保持翻译正常工作!

WebView webView = findViewById(R.id.about_text);
try {
  InputStream inputStream = getResources().openRawResource(R.raw.about);
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  StringBuilder stringBuilder = new StringBuilder();
  String line;
  while ((line = bufferedReader.readLine()) != null) {
    stringBuilder.append(line).append("\n");
  }
  webView.loadDataWithBaseURL(null, stringBuilder.toString(), "text/html", "UTF-8", null);
} catch (IOException e) {
  e.printStackTrace();
}