2013-07-03 88 views
0

我試圖在設備瀏覽器中加載本地html文件。我嘗試過使用WebView,但它不適用於所有設備。如何在Android瀏覽器中加載本地HTML文件

//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? 

爲我做到這一點的最好的辦法就是直接從資產文件夾,這是我所知道的是使用的WebView這麼簡單加載HTML,但就像我說這不工作像設備Galaxy Nexus和Nexus 7,它可以在像kindle fire和kindle hd這樣的設備上工作。

,但目前使用的方法IM是將其從資產文件夾到外部存儲器保存,並從那裏獲取文件通過的意圖,這犯規是因爲JavaScript的問題的工作負載。有什麼建議麼?謝謝

+0

您的webView是否啓用了JavaScript? –

+0

是的,你可以在後 – user1459976

回答

0

您是否嘗試過這些選項?

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

看到的只是想這些,也試過w.getSettings()。setPluginState(PluginState.ON)以及PluginState.ON_DEMAND。沒有工作 – user1459976

+0

@ user1459976你使用ProGruard嗎? –

+0

不,我不知道那是什麼 – user1459976

相關問題