我正在尝试使用shouldOverrideUrlLoading方法访问url。出于某种原因,当我尝试访问html文件中存在的url时,我得到网页不可用错误。以前我可以访问url因为我将html文件存储在raw文件夹中但我需要将其移动到资源文件夹,因为代码看起来更干净。这是我的代码,任何人都可以告诉我如何解决这个问题。
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file:///android_asset/myfile/file.html");
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
答案 0 :(得分:3)
Real Path
然后尝试这样:String YourURI="/android_asset/myfile/file.html";
YourURI = new File(getRealPathFromURI(YourURI));
//Got the Real Path Of the File
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(YourURI); //**Used the Real Path**
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
}
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Log.d(TAG, "No SDCARD");
}
else
{
webview.loadUrl("file://"+Environment.getExternalStorageDirectory()
+"/android_asset/myfile/file.html");
}
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}
}
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webview;
webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/myfile/file.html");
}
}
答案 1 :(得分:1)
还有另一种方式对你有用---
webView.loadData(customHtml, "text/html", "UTF-8");
其中custromHtml是用于显示的HTML行。
在“custromHtml”的地方你只需传递“file://android_asset/myfile/file.html”。
我认为这应该有用。
答案 2 :(得分:1)
您也可以尝试这一点 -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
myBrowser=(WebView)findViewById(R.id.mybrowser);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
并检查您是否已将文件放在主项目的正确文件夹中。如果一切正常,请制作一个干净的项目并修复项目。然后尝试运行它。
答案 3 :(得分:0)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
myBrowser=(WebView)findViewById(R.id.mybrowser);
myBrowser.getSettings().setJavaScriptEnabled(true);
myBrowser.setWebViewClient(new WebViewClient());
myBrowser.loadUrl(myURL);
}
答案 4 :(得分:0)
这样可以很好地尝试-----
"android.resource://[package]/[res type]/[res name]"
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
此链接也很有用 -
http://developer.android.com/reference/android/webkit/WebView.html
答案 5 :(得分:0)
shouldOverrideUrlLoading对于file://在ICS上不起作用,但对外部URL工作正常。 loadURL可以从assets文件夹中正常加载。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web=(WebView)findViewById(R.id.webView1);
web.setWebViewClient(new MyWebClient());
//web.loadUrl("file:///android_asset/index.html");
web.loadUrl("http://google.com");
.....
class MyWebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("MyWebClient", "url is: " + url);
if (url.equalsIgnoreCase("google.com")){
Log.d("MyWebClient","url is: " + url);
}
return false;
}
}
答案 6 :(得分:0)
我发现这个论坛解决方案为我解决了这个问题:http://www.cynosurex.com/Forums/DisplayComments.php?file=Java/Android/Android_4_has_a_.file....android_asset.webkit.._bug
答案 7 :(得分:-1)
我想你应该试试这个......
webview.setWebChromeClient(new WebChromeClient());
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("file://android_asset/myfile/file.html");
webview.setVerticalScrollBarEnabled(false);
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.equalsIgnoreCase("some text")){
setDialog("some fancy text");
}