如何在Android浏览器中加载本地HTML文件

时间:2013-07-03 18:03:10

标签: android html browser local

我正在尝试在设备浏览器中加载本地html文件。我尝试使用WebView,但它并不适用于所有设备。

//WebView method that didnt work for all devices
WebView w = new WebView(this);
w.getSettings().setJavaScriptEnabled(true);
w.loadUrl("file:///android_asset/my.html");
setContentView(w);

所以我尝试了这个方法,它在html查看器中打开文件,但是不起作用,因为我正在尝试加载的html需要javascript

// html view that also doesnt work
Intent i = new Intent();
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is optional
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(myFile), "text/*");
startActivity(i);

我至少花了一天的时间尝试不同的方法,比如添加

i.addCategory(Intent.CATEGORY_BROWSABLE);
i.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

但他们返回错误,如

Unable to start activity android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] typ=text/html }

Unable to start activity android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.browser/com.android.browser.BrowserActivity}; have you declared this activity in your AndroidManifest.xml?

我这样做的最好方法是直接从assets文件夹加载html,我知道使用WebView这么简单,但我说它不适用于像Galaxy Nexus和Nexus 7这样的设备它确实适用于kindle fire和kindle hd等设备。

但是目前我使用的方法是将它从assets文件夹保存到外部存储并从那里检索文件以通过intent加载,这不会因为javascript问题而起作用。有什么建议?感谢

1 个答案:

答案 0 :(得分:0)

您尝试过这些选项吗?

w.getSettings().setPluginsEnabled(true);
w.getSettings().setAllowFileAccess(true);